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

LINQ3: Translate All and Any in filters

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.14.0
    • Affects Version/s: None
    • Component/s: LINQ3
    • None

      There are many scenarios. The basic ones are:

      F.All(predicate) => { F : { $not : { $elemMatch : { $not : <predicate> } } } }
      F.Any() => { F : { $ne : null, $not : { $size : 0 } } }
      F.Any(predicate) => { F : { $elemMatch : <predicate> } } 

      The following are special cases that translate to $in or $all:

      a.All(i => F.Contains(i)) => { F : { $all : [<a>] } }
      a.Any(i => F.Contains(i)) => { F : { $in : [<a>] } }
      F.Any(i => a.Contains(i)) => { F : { $in : [<a>] } }
      
      // where a is an array constant and F is an array field

      All or Any may also be preceded by a Where:

      F.Where(p1).All(p2) => { F : { $not : { $elemMatch : { $not : <p1 && p2> } } } }
      F.Where(p1).Any() => { F : { $elemMatch : <p1> } }
      F.Where(p2).Any(p2) => { F : { $elemMatch : <p1 && p2> } } 

      All or Any may also be preceded by an OfType:

      F.OfType<T>().All(p) => { F : { $not : { $elemMatch : { $not : { $and : [{ _t : <disc> }, <p>] } } } } }
      F.OfType<T>().Any() => { F : { $elemMatch : { _t : <disc> } } }
      F.OfType<T>().Any(p) => { F : { $elemMatch : { $and : [{ _t : <disc> }, <p>] } } } 

      And finally, Where and OfType may both be used, possibly multiple times, in which case there are more than two predicates and the above generalizes to combine all the predicates.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: