0

I am using entity framework core. I am eager loading a navigation property for an entity. What I want to achieve here is only load top 5 records ordered by creation date along with the main entity. I have following query.

Context.Foo.Where(f => f.Id == fid)
    .Include(f1 => f1.Bars.OrderByDescending(b => b.CreateDate).Take(LIMIT))
    .FirstOrDefault();

It throws error that property expression is not valid. I understood that the expression should just evaluate to a property we want to include, but is there a way where we can fine tune the selection of properties?

Chaitanya Gadkari
  • 2,669
  • 4
  • 30
  • 54

1 Answers1

0

Filtering includes is not allowed in EF Core to the best on my knowledge. It does look like there is a github project that adds this feature which may work in your case. https://github.com/zzzprojects/EntityFramework-Plus/wiki/EF-Query-IncludeFilter-%7C-Entity-Framework-Include-Related-Entities-using-Where-Filter

  • Even though this is not what I am looking for, at the moment EF do not support the feature, so accepting this answer as its most suitable for now – Chaitanya Gadkari Feb 14 '18 at 09:10