it:ad:code_first:howto:queries:include_nested_tables

IT:AD:EF/CodeFirst:HowTo:Queries/Include Nested Tables

Summary

There are a couple of different ways to do it.

There's the old string way:

using (AppDbContext context = new AppDbContext())
{
var invoice = context.Invoices.Include("LineItems.Product").FirstOrDefault();
var product = invoice.LineItems.First().Product;
Debug.Assert(product!=null);
}

and the include/select:

Note how it can be nested indefinately…


using (AppDbContext context = new AppDbContext())
{
  var lineItem = context.Invoices
                        .Include(i => i.LineItems.Select(li => li.Product))
                        .FirstOrDefault();
  var product = invoice.LineItems.First().Product;
  Debug.Assert(product != null);
}

  • /home/skysigal/public_html/data/pages/it/ad/code_first/howto/queries/include_nested_tables.txt
  • Last modified: 2023/11/04 02:19
  • by 127.0.0.1