LINQKit
LINQKit copied to clipboard
PredicateBuilder inside Any clause doesn't work with pagination (Skip / Take)
Hello I will give a more detailed case as soon as I have enough time, but I came through an issue using PredicateBuilder (which is very handful and should be included straight inside EF Core I think). I use it to build a predicate to filter a navigation property (one to many). But doing do now breaks the pagination I use with Skip / Take, I've checked with SQL Profiler and all the results are returned. The filter works but it is done afterwards.
Basically I'm doing this (one Contact has many ContactDossier):
IQueryable<Models.Data.Contact> contacts = dbContext.Contacts;
var predicate = PredicateBuilder.True<Models.Data.ContactDossier>();
Expression<Func<Models.Data.ContactDossier, bool>> filterPredicate = cd => cd.Login == "test";
predicate = PredicateBuilder.And(predicate, filterPredicate);
var compiledPredicate = predicate.Compile();
Expression<Func<Models.Data.Contact, bool>> anyPredicate = contact => contact.ContactDossiers.Any(compiledPredicate);
var parentAnyPredicate = PredicateBuilder.New<Models.Data.Contact>().And(anyPredicate);
var compiledContactExpression = parentAnyPredicate .Compile();
contacts = contacts.Where(compiledContactExpression).AsQueryable();
var data = contacts .Skip(0).Take(10)
If I do it without PredicateBuilder, it works as expected (SQL query only returns the expected number of results) :
IQueryable<Models.Data.Contact> contacts = dbContext.Contacts;
contacts = contacts.Where(contact => contact.ContactDossiers.Any(cd => cd.Login == "test");
var data = contacts .Skip(0).Take(10)
Any idea ? Thanks
I could be wrong, but is this because you are missing AsExpandable()?