Skip to content
Merged
65 changes: 65 additions & 0 deletions source/fundamentals/linq.txt
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,71 @@ 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. The examples
in this section use the ``Guidebook`` class, which contain an array of ``Restaurant``
objects. The following code shows an example of a document modeled by the
``Guidebook`` class:

.. code-block:: json

{
"_id": { "$oid": ... },
"restaurants": [
{
"_id": { ... } ,
"address": { ... },
"name": "Tov Kosher Kitchen",
...
},
{
"_id": { ... } ,
"address": { ... },
"name": "Harriet's Kitchen",
...
},
...
]
}

The following code models the ``Guidebook`` class:

.. literalinclude:: /includes/fundamentals/code-examples/linq.cs
:language: csharp
:start-after: start-guidebook-model
:end-before: end-guidebook-model

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 of grades from all restaurants
in a document modeled by the ``Guidebook`` class.

.. 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

[
{ "date" : ISODate("2014-11-24T00:00:00Z"),
"grade" : "Z",
"score" : 20.0
},
{ "date" : ISODate("2013-01-17T00:00:00Z"),
"grade" : "A",
"score" : 13.0
},
...
]

$group
~~~~~~

Expand Down
14 changes: 14 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,20 @@ public class Ingredient

// end-ingredient-model

// start-guidebook-model

public class Guidebook
{
public List<Restaurant>? Restaurants { get; set; }
}

// end-guidebook-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