LINQ Interview Question #110

What is the role of Include in EF Core LINQ queries?

LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced

Quick Interview Answer

Include requests eager loading of related entity data when returning entity graphs. ThenInclude can continue through deeper relationships.

Detailed Explanation

Include requests eager loading of related entity data when returning entity graphs. ThenInclude can continue through deeper relationships.

Include is useful when the application genuinely needs entities and their related entities. For read-only DTO results, projection with Select can often be more targeted because only required fields are retrieved.

Code Example

var orders = await db.Orders
    .Include(o => o.Customer)
    .Include(o => o.Lines)
        .ThenInclude(l => l.Product)
    .ToListAsync();