Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions source/fundamentals/linq.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,96 @@ following documents:
{ "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 13 }
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }

Nested Statements
+++++++++++++++++

You can chain or nest ``Select`` and ``SelectMany`` statements to unwind nested
arrays. Consider a collection that contains documents with a **new** schema. These
documents contain a ``restaurants`` field, which holds an array of documents
represented by the ``Restaurant`` class. The documents within the array each have
a ``grades`` field that holds an array of documents represented by
the ``Grade`` class. The following code is an example of a single document in
this collection:

.. code-block:: json

{
"_id": { "$oid": ... },
"restaurants": [
{
"_id": { ... } ,
"address": { ... },
"name": "Tov Kosher Kitchen",
"grades": [
{
"date" : ISODate("2014-11-24T00:00:00Z"),
"grade" : "Z",
"score" : 20.0
},
{
"date" : ISODate("2013-01-17T00:00:00Z"),
"grade" : "A",
"score" : 13.0
}
]
...
},
{
"_id": { ... } ,
"address": { ... },
"name": "Harriet's Kitchen",
"grades": [
{
"date" : ISODate("2014-04-19T00:00:00Z"),
"grade" : "B",
"score" : 12.0
}
],
...
},
...
]
}

You can nest ``SelectMany`` statements within ``SelectMany`` or ``Select``
statements. The following example nests a ``SelectMany`` statement within a
``Select`` statement to retrieve an array from each document in the collection.
Each array holds all grade objects from all restaurants in each document.

.. io-code-block::
:copyable: true

.. input:: /includes/fundamentals/code-examples/linq.cs
:language: csharp
:start-after: start-nested-SelectMany
:end-before: end-nested-SelectMany

.. output::
:visible: false
:language: json

// output for first document in collection
[
{ "date" : ISODate("2014-11-24T00:00:00Z"),
"grade" : "Z",
"score" : 20.0
},
{ "date" : ISODate("2013-01-17T00:00:00Z"),
"grade" : "A",
"score" : 13.0
},
{
"date" : ISODate("2014-04-19T00:00:00Z"),
"grade" : "B",
"score" : 12.0
},
...
],
// output for second document in collection
[
...
]

$group
~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions source/includes/fundamentals/code-examples/linq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public class Ingredient

// end-ingredient-model

// start-nested-SelectMany
var query = queryableCollection
.Select(r => r.Restaurants.SelectMany(r => r.Grades));
// end-nested-SelectMany

// start-bitAnd-example

var query = queryableCollection
Expand Down