Skip to content

Commit 9fc51fd

Browse files
DOCSP-40066: Support SelectMany inside Project/Select (#373) (#400)
1 parent 00128e8 commit 9fc51fd

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

source/fundamentals/linq.txt

+90
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,96 @@ following documents:
320320
{ "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 13 }
321321
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }
322322

323+
Nested Statements
324+
+++++++++++++++++
325+
326+
You can chain or nest ``Select`` and ``SelectMany`` statements to unwind nested
327+
arrays. Consider a collection that contains documents with a **new** schema. These
328+
documents contain a ``restaurants`` field, which holds an array of documents
329+
represented by the ``Restaurant`` class. The documents within the array each have
330+
a ``grades`` field that holds an array of documents represented by
331+
the ``Grade`` class. The following code is an example of a single document in
332+
this collection:
333+
334+
.. code-block:: json
335+
336+
{
337+
"_id": { "$oid": ... },
338+
"restaurants": [
339+
{
340+
"_id": { ... } ,
341+
"address": { ... },
342+
"name": "Tov Kosher Kitchen",
343+
"grades": [
344+
{
345+
"date" : ISODate("2014-11-24T00:00:00Z"),
346+
"grade" : "Z",
347+
"score" : 20.0
348+
},
349+
{
350+
"date" : ISODate("2013-01-17T00:00:00Z"),
351+
"grade" : "A",
352+
"score" : 13.0
353+
}
354+
]
355+
...
356+
},
357+
{
358+
"_id": { ... } ,
359+
"address": { ... },
360+
"name": "Harriet's Kitchen",
361+
"grades": [
362+
{
363+
"date" : ISODate("2014-04-19T00:00:00Z"),
364+
"grade" : "B",
365+
"score" : 12.0
366+
}
367+
],
368+
...
369+
},
370+
...
371+
]
372+
}
373+
374+
You can nest ``SelectMany`` statements within ``SelectMany`` or ``Select``
375+
statements. The following example nests a ``SelectMany`` statement within a
376+
``Select`` statement to retrieve an array from each document in the collection.
377+
Each array holds all grade objects from all restaurants in each document.
378+
379+
.. io-code-block::
380+
:copyable: true
381+
382+
.. input:: /includes/fundamentals/code-examples/linq.cs
383+
:language: csharp
384+
:start-after: start-nested-SelectMany
385+
:end-before: end-nested-SelectMany
386+
387+
.. output::
388+
:visible: false
389+
:language: json
390+
391+
// output for first document in collection
392+
[
393+
{ "date" : ISODate("2014-11-24T00:00:00Z"),
394+
"grade" : "Z",
395+
"score" : 20.0
396+
},
397+
{ "date" : ISODate("2013-01-17T00:00:00Z"),
398+
"grade" : "A",
399+
"score" : 13.0
400+
},
401+
{
402+
"date" : ISODate("2014-04-19T00:00:00Z"),
403+
"grade" : "B",
404+
"score" : 12.0
405+
},
406+
...
407+
],
408+
// output for second document in collection
409+
[
410+
...
411+
]
412+
323413
$group
324414
~~~~~~
325415

source/includes/fundamentals/code-examples/linq.cs

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public class Ingredient
8181

8282
// end-ingredient-model
8383

84+
// start-nested-SelectMany
85+
var query = queryableCollection
86+
.Select(r => r.Restaurants.SelectMany(r => r.Grades));
87+
// end-nested-SelectMany
88+
8489
// start-bitAnd-example
8590

8691
var query = queryableCollection

0 commit comments

Comments
 (0)