Obtain object with Linq with its references

2

I have this query in LinQ, I filled the object with the query but it does not complete an object variable.

The model of Retención has a variable Empresa that is another model, and is the one that does not fill, remains null:

public class Retencion
{
    public int ID { get; set; }
    public int Año { get; set; }
    public string Nit { get; set; }
    public string Nombre { get; set; }
    public string Direccion { get; set; }
    public string Telefono { get; set; }
    public int Tipo { get; set; }
    public string Cuenta { get; set; }
    public string Concepto { get; set; }
    public int Base { get; set; }
    public int Porcentaje { get; set; }
    public int Valor { get; set; }

    public Empresa Empresa { get; set; }
    public int EmpresaId { get; set; }
}

public List<Retencion> Get(string nit, string ano)
{
    List<Retencion> result = new List<Retencion>();
    int a = Convert.ToInt32(ano);

    try
    {
        result = (from p in _context.Retencion
                  join e in _context.Empresa on p.EmpresaId equals e.ID
                  where (p.Año == a && p.Nit == nit)
                  select p).ToList();
    }
    catch (Exception e)
    {
        e.ToString();
    }

    return result;
}
    
asked by Sebastian Rico 21.02.2018 в 01:35
source

2 answers

1

At the moment of generating the query with Linq you need a conversion of an anonymous type (generated by Linq) to the type you want, in other words, Linq's query obtains an anonymous type and what you require is a type List<Retencion> . It is required to modify to the following:

result = (from p in _context.Retencion
            join e in _context.Empresa on p.EmpresaId equals e.ID
            where (p.Año == a && p.Nit == nit)
            select new Retencion
            {
                ID = p.ID,
                Año = p.Año,
                Nit = p.Nit,
                Nombre = p.Nombre,
                Direccion = p.Direccion,
                Telefono = p.Telefono,
                Tipo = p.Tipo,
                Cuenta = p.Cuenta,
                Concepto = p.Concept,
                Base = p.Base,
                Porcentaje = p.Porcentaje,
                Valor = p.Valor,
                Empresa = e,
                EmpresaId = e.EmpresaId
            }).ToList();

In case you are using Entity Framework, the task is simplified since you can include the references of each entity, for example, you could do something like this:

result = (from p in _context.Retencion
          join e in _context.Empresa on p.EmpresaId equals e.ID
          where (p.Año == a && p.Nit == nit)
          select p).Include("Empresa").ToList();

For this, you must make sure you have the reference to namespace using System.Data.Entity; .

    
answered by 21.02.2018 / 03:13
source
0
     result = (from p in _context.Retencion
              join e in _context.Empresa on p.EmpresaId equals e.ID
              _context.Retencion.Include("Empresa")
              where (p.Año == a && p.Nit == nit)
              select p).ToList();

I leave you this link with a similar question in English

    
answered by 21.02.2018 в 02:32