Home > Software engineering >  How to select an attribute with association ASP.NET Core 5.0
How to select an attribute with association ASP.NET Core 5.0

Time:07-26

I have the following entities into my project MnUsuario...

public MnUsuario()
{
    MnRolUsuarios = new HashSet<MnRolUsuario>();
    LogLogin = new HashSet<LogLogin>();
}

public int IdUsuario { get; set; }
public int IdEmpresa { get; set; }
public string Usuario { get; set; }
public string Login { get; set; }
public byte[] Password { get; set; }
public string Email { get; set; }

public virtual MnEmpresa IdEmpresaNavigation { get; set; }
public virtual ICollection<MnRolUsuario> MnRolUsuarios { get; set; }

public virtual ICollection<LogLogin> LogLogin { get; set; }

public string passwords { get; set; }
public int RolID { get; set; }
public bool RememberMe { get; set; }

MnEmpresa...

public partial class MnEmpresa
{
    public MnEmpresa()
    {
        MnAplicactivos = new HashSet<MnAplicactivo>();
        MnUsuarios = new HashSet<MnUsuario>();
    }

    public int IdEmpresa { get; set; }
    public int? CodEmpresa { get; set; }
    public string Empresa { get; set; }
    public string Contacto { get; set; }

    public string Imagen { get; set; }

    public virtual ICollection<MnAplicactivo> MnAplicactivos { get; set; }
    public virtual ICollection<MnUsuario> MnUsuarios { get; set; }
}

PBIContext...

public virtual DbSet<MnEmpresa> MnEmpresas { get; set; }
public virtual DbSet<MnUsuario> MnUsuarios { get; set; }

And my question here is... how can I access to the "Imagen" into MnEmpresas if i have the user MnUsuario, in SQL it's easy... Select with where condition for the relation, but in ASP.NET Core I don't know how to do it, I need to catch the image here...

MnUsuario user = null;

object[] parameters = new object[] { userName, password };

MnUsuario listad = _context.MnUsuarios.FirstOrDefault();

List<MnUsuario> listaUsuarios = await _context.MnUsuarios.FromSqlRaw<MnUsuario>("CALL sp_validate_login( {0}, {1})", parameters).ToListAsync();
user = listaUsuarios.FirstOrDefault();

if (user != null)
{
    var ids = _context.MnRolUsuarios
                      .Include(t => t.IdRolNavigation)
                      .ThenInclude(t => t.IdAplicativoNavigation)
                      .ThenInclude(t => t.IdEmpresaNavigation)
                      .Where(x => x.IdUsuario == user.IdUsuario)
                      .Select(x => new { x.IdRol, x.IdRolNavigation.IdAplicativoNavigation.IdEmpresaNavigation.Imagen });

     string consRolID = string.Join(',', ids.Select(x => x.IdRol).ToArray());
     string imagen = ids.FirstOrDefault()?.Imagen ?? string.Empty;
     string img = // Code here
}

Where it's the comment "Code here"

CodePudding user response:

If you change:

var ids = _context.MnRolUsuarios.Include(t => t.IdRolNavigation).ThenInclude(t => t.IdAplicativoNavigation)
                               .ThenInclude(t => t.IdEmpresaNavigation)
                               .Where(x => x.IdUsuario == user.IdUsuario)
                               .Select(x => new { x.IdRol, x.IdRolNavigation.IdAplicativoNavigation.IdEmpresaNavigation.Imagen });//give the property a name

To:

    var ids = _context.MnRolUsuarios.Include(t => t.IdRolNavigation).ThenInclude(t => t.IdAplicativoNavigation)
                               .ThenInclude(t => t.IdEmpresaNavigation)
                               .Where(x => x.IdUsuario == user.IdUsuario)
                               .Select(x => new { x.IdRol, Imagen: x.IdRolNavigation.IdAplicativoNavigation.IdEmpresaNavigation.Imagen });

Then you could use the line above the comment.

CodePudding user response:

If you wanna return a anonymous type results in linq, you need to use = instead of ::

var ids = _context.MnRolUsuarios.Include(t => t.IdRolNavigation).ThenInclude(t => t.IdAplicativoNavigation)
                               .ThenInclude(t => t.IdEmpresaNavigation)
                               .Where(x => x.IdUsuario == user.IdUsuario)
                               .Select(x => new { x.IdRol, Imagen = x.IdRolNavigation.IdAplicativoNavigation.IdEmpresaNavigation.Imagen }); 

Then you can use ids.FirstOrDefault()?.Imagen to get the Imagen

  • Related