How to use Include for objects that are within a list

1

I have the following objects in my EF6 context.

public class Produto
{
    public Guid Id {get;set;}
    public string Nome {get;set;}
}

public class VedaItem
{
    public int Item {get;set;}
    public float Valor {get;set;}
    public Guid ProdutoId {get;set;}

    public virtual Produto Produto {get;set;}
}

public class Venda
{
    public DateTime DataVenda {get;set;}
    public string Numero {get;set;}
    public List<VendaItem> Itens {get;set;}

} 

I do not use LazyLoading, so I need to use Include to access relationships with Venda to generate a report.

I need to access Product data that is in VendaItem .

I tried to include it as follows.

var relatorio = contexto.Set<Venda>()
     .Include(v => v.Itens.Produto);
     .Select(v => new 
     {
        NomeProduto = v.Itens.Produto.Nome
     });

But VisualStudio does not even allow me to type this: v => v.Itens.Produto .

How do I get access to data from the Product entity?

    
asked by anonymous 20.04.2018 / 21:40

1 answer

1

It would look like this:

 var relatorio = contexto.Set<Venda>()
                 .Include(v => v.Itens.Select(p=> p.Produto));

Will return the sales list, with items and products loaded.

    
20.04.2018 / 21:46