Skip to content

Commit c2adeca

Browse files
authored
DOCSP-48356: skipwhile & takewhile (#536)
* DOCSP-48356: skipwhile & takewhile * copyable false * RStam tech review * RStam tech review 2
1 parent ca969d4 commit c2adeca

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

source/fundamentals/linq.txt

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,44 @@ The result of the preceding example contains the following documents:
248248
{ "name" : "Crystal Room", "cuisine" : "Italian" }
249249
{ "name" : "Forlinis Restaurant", "cuisine" : "Italian" }
250250

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+
251289
$sample
252290
~~~~~~~
253291

@@ -301,6 +339,44 @@ returns the rest. The result contains the following documents:
301339
{ "name" : "Forlinis Restaurant", "cuisine" : "Italian" }
302340
...
303341

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+
304380
$unwind
305381
~~~~~~~
306382

@@ -346,7 +422,7 @@ following documents:
346422
{ "date" : ISODate("2012-05-17T00:00:00Z"), "grade" : "A", "score" : 11 }
347423

348424
Nested Statements
349-
+++++++++++++++++
425+
`````````````````
350426

351427
You can chain or nest ``Select`` and ``SelectMany`` statements to unwind nested
352428
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
558634
``$lookup`` by using each method.
559635

560636
Lookup()
561-
++++++++
637+
````````
562638

563639
The following code specifies a ``$lookup`` stage by using the ``Lookup()``
564640
method. This example joins documents from the ``reviews`` collection to
@@ -768,7 +844,7 @@ collection:
768844
throw an error.
769845

770846
$bitAnd
771-
+++++++
847+
```````
772848

773849
The ``$bitAnd`` aggregation operator performs a bitwise AND operation on the given
774850
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
9851061
* - ``Skip``
9861062
- Skips over a specified number of documents and returns the rest of the results
9871063

1064+
* - ``SkipWhile`` (only in ``Select`` projections)
1065+
- Skips over array elements while a condition is true and returns
1066+
the remaining elements
1067+
9881068
* - ``Sum``
9891069
- Returns the sum of the values in a specified field
9901070

9911071
* - ``Take``
9921072
- Specifies the number of results to return
9931073

1074+
* - ``TakeWhile`` (only in ``Select`` projections)
1075+
- Returns array elements while a condition is true and skips
1076+
the remaining elements
1077+
9941078
* - ``Where``
9951079
- Returns all documents that match your specified criteria
9961080

@@ -1079,4 +1163,5 @@ API Documentation
10791163
For a complete list of supported LINQ methods, see the following API documentation:
10801164

10811165
- `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>`__

source/whats-new.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ The 3.3 driver release includes the following new features:
6464
- Adds the following LINQ features:
6565

6666
- Support for the ``SkipWhile()`` and ``TakeWhile()`` LINQ
67-
aggregation methods.
67+
aggregation methods. To learn more, see the
68+
:ref:`csharp-linq-skipwhile` and :ref:`csharp-linq-takewhile`
69+
sections of the LINQ guide.
6870

6971
- Support for the ``$sigmoid`` expression in LINQ queries.
7072

0 commit comments

Comments
 (0)