Skip to content

Commit b69bfd8

Browse files
DOCSP-46188 Add Lookup() method to LINQ docs (#522)
1 parent 057727c commit b69bfd8

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

source/fundamentals/linq.txt

+49-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ LINQ Syntax for Aggregation Operations
1717
.. meta::
1818
:keywords: code example, query, aggregation
1919

20-
2120
Overview
2221
--------
2322

@@ -541,13 +540,6 @@ from another collection in the same database. The ``$lookup`` stage adds a new
541540
array field to each input document. The new array field contains the matching
542541
documents from the "joined" collection.
543542

544-
.. note::
545-
546-
To perform a lookup, you must make both collections queryable by using the
547-
``AsQueryable()`` method.
548-
549-
To learn how to make a collection queryable, see :ref:`csharp-linq-queryable`.
550-
551543
Consider a second collection in the ``sample_restaurants`` database called
552544
``reviews`` that has restaurant reviews. You can join documents from that collection
553545
to documents with the same ``name`` value in the ``restaurants`` collection using
@@ -561,6 +553,55 @@ The following ``Review`` class models the documents in the ``reviews`` collectio
561553
:start-after: start-review-model
562554
:end-before: end-review-model
563555

556+
You can specify a ``$lookup`` stage by calling the ``Lookup()`` method
557+
or the ``GroupJoin()`` method. The following sections show how to perform a
558+
``$lookup`` by using each method.
559+
560+
Lookup()
561+
++++++++
562+
563+
The following code specifies a ``$lookup`` stage by using the ``Lookup()``
564+
method. This example joins documents from the ``reviews`` collection to
565+
documents from the ``restaurants`` collection where the ``RestaurantName`` field in the
566+
``reviews`` collection matches the ``Name`` field in the ``restaurants`` collection:
567+
568+
.. code-block:: csharp
569+
570+
var lookupResult = restaurantsCollection.AsQueryable()
571+
.Lookup(reviewCollection,
572+
restaurant => restaurant.Name,
573+
review => review.RestaurantName);
574+
575+
The preceding example returns a list of ``LookupResult`` objects that
576+
each contain a joined document. To learn more about the ``LookupResult`` class,
577+
see the `LookupResult API documentation
578+
<{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.LookupResult-2.html>`__.
579+
580+
You can also use a ``Lookup()`` method overload to specify additional criteria
581+
for the join. The following example joins documents from the ``restaurants``
582+
collection to documents from the ``reviews`` collection where the
583+
``RestaurantName`` field in the ``reviews`` collection matches the ``Name``
584+
field in the ``restaurants`` collection and the ``ReviewText`` field in the
585+
``reviews`` collection contains the name of the restaurant:
586+
587+
.. code-block:: csharp
588+
589+
var lookupResult = restaurantsCollection.AsQueryable()
590+
.Lookup(reviewCollection,
591+
(restaurant, reviews) => reviews
592+
.Where(review => review.ReviewText.Contains(restaurant.Name)));
593+
594+
To view a full list of overloads for the ``Lookup()`` method, see the `Lookup
595+
API documentation <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Linq.MongoQueryable.Lookup.html>`__.
596+
597+
GroupJoin()
598+
+++++++++++
599+
600+
You can specify a ``$lookup`` stage by using the LINQ ``GroupJoin()`` method. To
601+
perform a ``GroupJoin()`` lookup, you must make both collections queryable by
602+
using the ``AsQueryable()`` method. To learn how to make a collection queryable,
603+
see :ref:`csharp-linq-queryable`.
604+
564605
Select the :guilabel:`Method Syntax` or :guilabel:`Query Syntax` tab to see how
565606
to generate a ``$lookup`` stage by using LINQ:
566607

0 commit comments

Comments
 (0)