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

Support SelectMany inside Project/Select

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • 2.26.0
    • Affects Version/s: None
    • Component/s: LINQ3
    • None
    • Fully Compatible
    • Dotnet Drivers
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Optionally document that SelectMany is now supported nested inside a Select/Project.

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to? Optionally document that SelectMany is now supported nested inside a Select/Project.

      The following two LINQ queries are equivalent:

      var linq3Chained = coll.AsQueryable().SelectMany(series => series.Books).SelectMany(book => book.Chapters).Select(chapter => chapter.Title);
      Console.WriteLine(linq3Chained);
      var linq3Nested = coll.AsQueryable().SelectMany(series => series.Books.SelectMany(book => book.Chapters.Select(chapter => chapter.Title)));
      Console.WriteLine(linq3Nested);
      

      The first query outputs:

      test.library.Aggregate([{ "$project" : { "_v" : "$Books", "_id" : 0 } }, { "$unwind" : "$_v" }, { "$project" : { "_v" : "$_v.Chapters", "_id" : 0 } }, { "$unwind" : "$_v" }, { "$project" : { "_v" : "$_v.Title", "_id" : 0 } }])
      

      The second query outputs:

      Expression not supported: series.Books.SelectMany(book => book.Chapters.Select(chapter => chapter.Title)).
      

      While the second query can be re-written in the form of the first, this cannot always be done if the LINQ3 provider is being used to translate an expression used in a builder (Project, Search, etc.) to a projection. The following aggregation also throws ExpressionNotSupportedException:

      var aggNested = coll.Aggregate().Project(series => series.Books.SelectMany(book => book.Chapters.Select(chapter => chapter.Title)));
      Console.WriteLine(aggNested);
      

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

              Created:
              Updated:
              Resolved: