-
Type:
New Feature
-
Resolution: Unresolved
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 2.18.0
-
Component/s: LINQ3
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Summary
Filtering joined collections only works with inline predicates/lamba expressions.
Example
This works:
await _personCollection.AsQueryable() .GroupJoin(_carCollection.AsQueryable(), r => r.PersonId, a => a.PersonId, (p, c) => new { person = p, cars = c.Where(t => t.IsActive == true).DefaultIfEmpty() }) .Select(t => new PersonReportDto { PersonName = person.Name, Cars = cars }) .ToListAsync();
Notice the inline t => t.IsActive == true
This doesn't work:
var carFilterDefinition = Builders<Car>.Filter.Where(/* my custom predicate (Expression<Func<>>), received by parameter */); await _personCollection.AsQueryable() .GroupJoin(_carCollection.AsQueryable(), r => r.PersonId, a => a.PersonId, (p, c) => new { person = p, cars = c.Where(_ => carFilterDefinition.Inject()).DefaultIfEmpty() }) .Select(t => new PersonReportDto { PersonName = person.Name, Cars = cars }) .ToListAsync(); // Runtime error: Expression not supported: Inject()
Also is worth mentioning that passing an Expression<Func<TDocument, bool>> parameter directly to the Where }}is not possible due to {{cars collection being of type IEnumerable<Car> and not IQueryable<Car>