Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4491

Filtering joined collections only works with inline predicates

    XMLWordPrintableJSON

Details

    • Icon: New Feature New Feature
    • Resolution: Unresolved
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.18.0
    • LINQ3
    • 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>

      Attachments

        Activity

          People

            Unassigned Unassigned
            alexandre.murari@flashapp.com.br Alexandre Junior
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: