Skip to content
Merged
84 changes: 84 additions & 0 deletions source/fundamentals/linq.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,90 @@ 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
that each represent the ``Restaurant`` class. The documents within the array each have
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: since the classes model the documents, I think it's more accurate to say the document is "represented by" Restaurants, rather than represent Restaurants

Suggested change
that each represent the ``Restaurant`` class. The documents within the array each have
represented by the ``Restaurant`` class. The documents within the array each have

a ``grades`` field that holds an array of documents that each represent
the ``Grade`` class. The following code is an example of a single document in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

S: same suggestion as above

Suggested change
a ``grades`` field that holds an array of documents that each represent
the ``Grade`` class. The following code is an example of a single document in
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": [ ... ],
...
},
...
]
}

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: should this be the first restaurant instead? since these are just the grades for Tov Kosher Kitchen, but the first guidebook document would have more restaurants

Suggested change
// output for first document in collection
// output for first restaurant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it should be first document - I will make the example/output more clear

[
{ "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("..."),
"grade" : ...,
"score" : ...
},
...
],
// 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
Loading