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);
- duplicates
-
CSHARP-4879 Support SelectMany method in LINQ
- Closed