Skip to content

Commit 7d80701

Browse files
committed
initial commit
1 parent 7f3efc0 commit 7d80701

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

source/fundamentals/linq.txt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,23 @@ The result of the preceding example contains the following document:
187187
If you don't include the ``_id`` field in your LINQ projection, the {+driver-short+}
188188
automatically excludes it from the results.
189189

190+
You can chain and nest
191+
192+
``Select`` supports nested ``SelectMany`` statements. Y
193+
194+
195+
196+
197+
198+
The following code shows
199+
how to nest ``Select`` and ``SelectMany`` statements:
200+
201+
.. code-block:: csharp
202+
203+
var query = queryableCollection
204+
.Select(r => r.Grades.SelectMany(grade => new {
205+
restaurant.Name, restaurant.Grades }));
206+
190207
$match
191208
~~~~~~
192209

@@ -320,6 +337,70 @@ following documents:
320337
{ "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 13 }
321338
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }
322339

340+
Nested Statements
341+
+++++++++++++++++
342+
343+
You can chain or nest ``Select`` and ``SelectMany`` statements. The examples
344+
in this section use the ``Guidebook`` class, which contain an array of ``Restaurant``
345+
objects. The following code shows an example of a document modeled by the
346+
``Guidebook`` class:
347+
348+
.. code-block:: json
349+
350+
{
351+
"_id": { "$oid": ... },
352+
"restaurants": [
353+
{
354+
"_id": { ... } ,
355+
"address": { ... },
356+
"name": "Tov Kosher Kitchen",
357+
...
358+
},
359+
{
360+
"_id": { ... } ,
361+
"address": { ... },
362+
"name": "Harriet's Kitchen",
363+
...
364+
},
365+
...
366+
]
367+
}
368+
369+
The following code models the ``Guidebook`` class:
370+
371+
.. literalinclude:: /includes/fundamentals/code-examples/linq.cs
372+
:start-after: start-guidebook-model
373+
:end-before: end-guidebook-model
374+
375+
You can nest ``SelectMany`` statements within ``SelectMany`` or ``Select``
376+
statements. The following example nests a ``SelectMany`` statement within a
377+
``Select`` statement to retrieve an array of grades from all restaurants
378+
in a document modeled by the ``Guidebook`` class.
379+
380+
.. io-code-block::
381+
:copyable: true
382+
383+
.. input:: /includes/fundamentals/code-examples/linq.cs
384+
:language: csharp
385+
:start-after: start-nested-SelectMany
386+
:end-before: end-nested-SelectMany
387+
388+
.. output::
389+
:visible: false
390+
:language: json
391+
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+
]
403+
323404
$group
324405
~~~~~~
325406

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ public class Ingredient
8181

8282
// end-ingredient-model
8383

84+
// start-guidebook-model
85+
86+
public class Guidebook
87+
{
88+
public List<Restaurant>? Restaurants { get; set; }
89+
}
90+
91+
// end-guidebook-model
92+
93+
// start-nested-SelectMany
94+
var query = queryableCollection
95+
.Select(r => r.Restaurants.SelectMany(r => r.Grades));
96+
// end-nested-SelectMany
97+
8498
// start-bitAnd-example
8599

86100
var query = queryableCollection

0 commit comments

Comments
 (0)