@@ -187,6 +187,23 @@ The result of the preceding example contains the following document:
187
187
If you don't include the ``_id`` field in your LINQ projection, the {+driver-short+}
188
188
automatically excludes it from the results.
189
189
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
+
190
207
$match
191
208
~~~~~~
192
209
@@ -320,6 +337,70 @@ following documents:
320
337
{ "date" : ISODate("2012-12-05T00:00:00Z"), "grade" : "A", "score" : 13 }
321
338
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }
322
339
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
+
323
404
$group
324
405
~~~~~~
325
406
0 commit comments