EF Core by Patrik

Filtering on Include in EF Core

Entity Framework Core 5 is the first EF version to support filtering in Include.

Supported operations are Where, OrderBy(Descending)/ThenBy(Descending), Skip, Take

Some usage example

context.Customers
    .Include(c => c.Orders.Where(o => o.Name != "Foo")).ThenInclude(o => o.OrderDetails)
    .Include(c => c.Orders).ThenInclude(o => o.Customer)

Only one filter is allowed per navigation, so for cases where the same navigation needs to be included multiple times (e.g. multiple ThenInclude on the same navigation) apply the filter only once, or apply exactly the same filter for that navigation.

Comments

Leave a Comment

All fields are required. Your email address will not be published.