@@ -17,7 +17,6 @@ LINQ Syntax for Aggregation Operations
17
17
.. meta::
18
18
:keywords: code example, query, aggregation
19
19
20
-
21
20
Overview
22
21
--------
23
22
@@ -541,13 +540,6 @@ from another collection in the same database. The ``$lookup`` stage adds a new
541
540
array field to each input document. The new array field contains the matching
542
541
documents from the "joined" collection.
543
542
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
-
551
543
Consider a second collection in the ``sample_restaurants`` database called
552
544
``reviews`` that has restaurant reviews. You can join documents from that collection
553
545
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
561
553
:start-after: start-review-model
562
554
:end-before: end-review-model
563
555
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
+
564
605
Select the :guilabel:`Method Syntax` or :guilabel:`Query Syntax` tab to see how
565
606
to generate a ``$lookup`` stage by using LINQ:
566
607
0 commit comments