@@ -248,6 +248,44 @@ The result of the preceding example contains the following documents:
248
248
{ "name" : "Crystal Room", "cuisine" : "Italian" }
249
249
{ "name" : "Forlinis Restaurant", "cuisine" : "Italian" }
250
250
251
+ .. _csharp-linq-takewhile:
252
+
253
+ Limit on a Condition
254
+ ````````````````````
255
+
256
+ You can use the ``TakeWhile()`` LINQ method in a ``Select()`` projection
257
+ to return array field elements while a specified condition is true, then
258
+ skip the remaining elements.
259
+
260
+ This example uses the following ``Student`` class to model documents
261
+ that contain an array field ``Grades``:
262
+
263
+ .. code-block:: csharp
264
+
265
+ public class Student
266
+ {
267
+ public ObjectId Id { get; set; }
268
+ public string Name { get; set; }
269
+ public int[] Grades { get; set; }
270
+ }
271
+
272
+ The following code shows how to use the ``TakeWhile()`` method to return
273
+ any ``Grades`` array elements that are greater than ``90`` and skip the
274
+ rest of the array:
275
+
276
+ .. code-block:: csharp
277
+
278
+ var query = queryableCollection
279
+ .Select(s => s.Grades.TakeWhile(g => g > 90).ToArray());
280
+
281
+ The results might resemble the following arrays:
282
+
283
+ .. code-block:: json
284
+ :copyable: false
285
+
286
+ [92, 97]
287
+ [100, 95, 91]
288
+
251
289
$sample
252
290
~~~~~~~
253
291
@@ -301,6 +339,44 @@ returns the rest. The result contains the following documents:
301
339
{ "name" : "Forlinis Restaurant", "cuisine" : "Italian" }
302
340
...
303
341
342
+ .. _csharp-linq-skipwhile:
343
+
344
+ Skip on a Condition
345
+ ```````````````````
346
+
347
+ You can use the ``SkipWhile()`` LINQ method in a ``Select()`` projection
348
+ to skip array field elements while a specified condition is true, then
349
+ return the remaining elements.
350
+
351
+ This example uses the following ``Student`` class to model documents
352
+ that contain an array field ``Grades``:
353
+
354
+ .. code-block:: csharp
355
+
356
+ public class Student
357
+ {
358
+ public ObjectId Id { get; set; }
359
+ public string Name { get; set; }
360
+ public int[] Grades { get; set; }
361
+ }
362
+
363
+ The following code shows how to use the ``SkipWhile()`` method to skip
364
+ any ``Grades`` array elements that are less than ``75`` and return the
365
+ rest of the array:
366
+
367
+ .. code-block:: csharp
368
+
369
+ var query = queryableCollection
370
+ .Select(s => s.Grades.SkipWhile(g => g < 75).ToArray());
371
+
372
+ The results might resemble the following arrays:
373
+
374
+ .. code-block:: json
375
+ :copyable: false
376
+
377
+ [80, 90, 70, 83]
378
+ [79, 100, 85, 73]
379
+
304
380
$unwind
305
381
~~~~~~~
306
382
@@ -346,7 +422,7 @@ following documents:
346
422
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }
347
423
348
424
Nested Statements
349
- +++++++++++++++++
425
+ `````````````````
350
426
351
427
You can chain or nest ``Select`` and ``SelectMany`` statements to unwind nested
352
428
arrays. Consider a collection that contains documents with a **new** schema. These
@@ -558,7 +634,7 @@ or the ``GroupJoin()`` method. The following sections show how to perform a
558
634
``$lookup`` by using each method.
559
635
560
636
Lookup()
561
- ++++++++
637
+ ````````
562
638
563
639
The following code specifies a ``$lookup`` stage by using the ``Lookup()``
564
640
method. This example joins documents from the ``reviews`` collection to
@@ -768,7 +844,7 @@ collection:
768
844
throw an error.
769
845
770
846
$bitAnd
771
- +++++++
847
+ ```````
772
848
773
849
The ``$bitAnd`` aggregation operator performs a bitwise AND operation on the given
774
850
arguments. You can use the ``$bitAnd`` operator by connecting two or more
@@ -985,12 +1061,20 @@ The following are some methods supported by the {+driver-long+} implementation o
985
1061
* - ``Skip``
986
1062
- Skips over a specified number of documents and returns the rest of the results
987
1063
1064
+ * - ``SkipWhile`` (only in ``Select`` projections)
1065
+ - Skips over array elements while a condition is true and returns
1066
+ the remaining elements
1067
+
988
1068
* - ``Sum``
989
1069
- Returns the sum of the values in a specified field
990
1070
991
1071
* - ``Take``
992
1072
- Specifies the number of results to return
993
1073
1074
+ * - ``TakeWhile`` (only in ``Select`` projections)
1075
+ - Returns array elements while a condition is true and skips
1076
+ the remaining elements
1077
+
994
1078
* - ``Where``
995
1079
- Returns all documents that match your specified criteria
996
1080
@@ -1079,4 +1163,5 @@ API Documentation
1079
1163
For a complete list of supported LINQ methods, see the following API documentation:
1080
1164
1081
1165
- `LINQ <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.html>`__
1082
- - `MongoQueryable <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.MongoQueryable.html>`__
1166
+ - `MongoQueryable
1167
+ <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.MongoQueryable.html>`__
0 commit comments