Skip to content

Commit 2c6a096

Browse files
Provide Perform inner joins article with method-based syntax examples (#36376)
* Implement method syntax approach for each inner join example * Invoke the method-syntax methods in Program * Adjust tests to cover the equality between query and method syntax results * Add the method-syntax examples to the Inner Joins page * Correct capitalization of a note for grouped joins Co-authored-by: Bill Wagner <[email protected]> * Use 2nd person for advices in Inner joins docs Co-authored-by: Bill Wagner <[email protected]> --------- Co-authored-by: Bill Wagner <[email protected]>
1 parent a06979c commit 2c6a096

File tree

4 files changed

+250
-35
lines changed

4 files changed

+250
-35
lines changed

docs/csharp/linq/perform-inner-joins.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ The following example creates two collections that contain objects of two user-d
3131

3232
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_1":::
3333

34+
You achieve the same results using the <xref:System.Linq.Enumerable.Join%2A> method syntax:
35+
36+
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_method_syntax_1":::
37+
3438
Note that the `Person` object whose `LastName` is "Huff" does not appear in the result set because there is no `Pet` object that has `Pet.Owner` equal to that `Person`.
3539

3640
## Example - Composite key join
@@ -41,6 +45,10 @@ The following example uses a list of `Employee` objects and a list of `Student`
4145

4246
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_2":::
4347

48+
You can use the <xref:System.Linq.Enumerable.Join%2A> method, as shown in the following example:
49+
50+
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_method_syntax_2":::
51+
4452
## Example - Multiple join
4553

4654
Any number of join operations can be appended to each other to perform a multiple join. Each `join` clause in C# correlates a specified data source with the results of the previous join.
@@ -53,6 +61,10 @@ The second `join` clause in C# correlates the anonymous types returned by the fi
5361

5462
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_3":::
5563

64+
The equivalent using multiple <xref:System.Linq.Enumerable.Join%2A> method uses the same approach with the anonymous type (in the example below it's named `commonOwner`):
65+
66+
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_method_syntax_3":::
67+
5668
## Example - Inner join by using grouped join
5769

5870
The following example shows you how to implement an inner join by using a group join.
@@ -65,6 +77,14 @@ The result of `query1` is equivalent to the result set that would have been obta
6577

6678
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_4":::
6779

80+
The same results can be achieved using <xref:System.Linq.Enumerable.GroupJoin%2A> method, as follows:
81+
82+
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_method_syntax_4":::
83+
84+
Note that this approach requires chaining the query results with <xref:System.Linq.Enumerable.SelectMany%2A> to create the final list of Owner - Pet relation based on the results of group join. To avoid chaining, the single <xref:System.Linq.Enumerable.Join%2A> method can be used as presented below:
85+
86+
:::code language="csharp" source="../../../samples/snippets/csharp/concepts/linq/LinqSamples/InnerJoins.cs" id="inner_joins_method_syntax_5":::
87+
6888
## See also
6989

7090
- <xref:System.Linq.Enumerable.Join%2A>

samples/snippets/csharp/concepts/linq/LinqSamples.Test/InnerJoinsTest.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,54 @@ public class InnerJoinsTest
88
[Fact]
99
public void BasicTest()
1010
{
11-
var sw = InitTest();
12-
13-
InnerJoins.Basic();
14-
Assert.Equal(
11+
const string expectedOutput =
1512
@"""Daisy"" is owned by Magnus
1613
""Barley"" is owned by Terry
1714
""Boots"" is owned by Terry
1815
""Whiskers"" is owned by Charlotte
1916
""Blue Moon"" is owned by Rui
20-
", sw.ToString());
17+
";
18+
19+
var querySyntaxResult = InnerJoins.Basic();
20+
Assert.Equal(expectedOutput, querySyntaxResult);
21+
22+
var methodSyntaxResult = InnerJoins.BasicMethodSyntax();
23+
Assert.Equal(methodSyntaxResult, querySyntaxResult);
2124
}
2225

2326
[Fact]
2427
public void CompositeKeyTest()
2528
{
26-
var sw = InitTest();
27-
28-
InnerJoins.CompositeKey();
29-
Assert.Equal(
29+
const string expectedOutput =
3030
@"The following people are both employees and students:
3131
Terry Adams
3232
Vernette Price
33-
", sw.ToString());
33+
";
34+
var querySyntaxResult = InnerJoins.CompositeKey();
35+
Assert.Equal(expectedOutput, querySyntaxResult);
36+
37+
var methodSyntaxResult = InnerJoins.CompositeKeyMethodSyntax();
38+
Assert.Equal(methodSyntaxResult, querySyntaxResult);
3439
}
3540

3641
[Fact]
3742
public void MultipleJoinTest()
3843
{
39-
var sw = InitTest();
40-
41-
InnerJoins.MultipleJoin();
42-
Assert.Equal(
44+
const string expectedOutput =
4345
@"The cat ""Daisy"" shares a house, and the first letter of their name, with ""Duke"".
4446
The cat ""Whiskers"" shares a house, and the first letter of their name, with ""Wiley"".
45-
", sw.ToString());
47+
";
48+
var querySyntaxResult = InnerJoins.MultipleJoin();
49+
Assert.Equal(expectedOutput, querySyntaxResult);
50+
51+
var methodSyntaxResult = InnerJoins.MultipleJoinMethodSyntax();
52+
Assert.Equal(methodSyntaxResult, querySyntaxResult);
4653
}
4754

4855
[Fact]
4956
public void InnerGroupJoinTest()
5057
{
51-
var sw = InitTest();
52-
53-
InnerJoins.InnerGroupJoin();
54-
Assert.Equal(
58+
const string expectedOutput =
5559
@"Inner join using GroupJoin():
5660
Magnus - Daisy
5761
Terry - Barley
@@ -65,6 +69,11 @@ public void InnerGroupJoinTest()
6569
Terry - Boots
6670
Terry - Blue Moon
6771
Charlotte - Whiskers
68-
", sw.ToString());
72+
";
73+
var querySyntaxResult = InnerJoins.InnerGroupJoin();
74+
Assert.Equal(expectedOutput, querySyntaxResult);
75+
76+
var methodSyntaxResult = InnerJoins.InnerGroupJoinMethodSyntax();
77+
Assert.Equal(methodSyntaxResult, querySyntaxResult);
6978
}
7079
}

0 commit comments

Comments
 (0)