Details
-
New Feature
-
Resolution: Unresolved
-
Minor - P4
-
None
-
2.18.0
-
None
Description
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>