Entity framework LINQ w/ nested table

Costas

Administrator
Staff member
Using Lambda Expression
JavaScript:
using (var context = new EntityContext())
{
    var customers = context.Customers
            .Include(i => i.Invoices.Select(it => it.Items))
            .ToList();
}

Using String Path
JavaScript:
using (var context = new EntityContext())
{
    var customers = context.Customers
            .Include("Invoices.Items")
            .ToList();
}

Also the two of them will load all customers, their related invoices, and the items of each invoice.


In Entity Framework Core use .ThenInclude
JavaScript:
//src - https://stackoverflow.com/a/47555838
//ref - https://docs.microsoft.com/en-us/ef/core/querying/related-data
var blogs = context.Blogs
    .Include(blog => blog.Posts)
        .ThenInclude(post => post.Author)
    .ToList();


src - https://entityframework.net/include-multiple-levels
 
Top