Entity Framework Core Interview Question #39
What are Include and ThenInclude?
Relationships, Navigation Properties & Loading Data Junior Beginner
Quick Interview Answer
Include specifies a related navigation to load with the main query. ThenInclude continues from an included navigation to load another level of related data.
Detailed Explanation
Include specifies a related navigation to load with the main query. ThenInclude continues from an included navigation to load another level of related data.
They are useful for loading entity graphs, such as Orders with Items and each Item's Product.
Deep or repeated includes can produce large queries. For read-only screens, projecting directly into a DTO can often retrieve a smaller and more efficient shape.
Code Example
var orders = await db.Orders
.Include(o => o.Items)
.ThenInclude(i => i.Product)
.Include(o => o.Customer)
.ToListAsync();