Skip to content

Commit 818d268

Browse files
Add UnionBy documentation examples (#12518)
1 parent 2156aae commit 818d268

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,5 +3277,85 @@ public static void AggregateBySeedExample()
32773277
// </Snippet206>
32783278
}
32793279
#endregion
3280+
3281+
#region UnionBy
3282+
static class UnionBy
3283+
{
3284+
// <Snippet207>
3285+
public static void UnionByKeySelectorExample()
3286+
{
3287+
(int ProductId, string Name , decimal Price)[] localProducts =
3288+
{
3289+
(101, "Laptop", 1000m),
3290+
(102, "Mouse", 100m),
3291+
(103, "Keyboard", 120m)
3292+
};
3293+
3294+
(int ProductId, string Name, decimal Price)[] warehouseProducts =
3295+
{
3296+
(102, "Mouse", 100m), // Duplicate ProductId (already in local)
3297+
(104, "Monitor", 800m),
3298+
(101, "Laptop", 1000m) // Duplicate ProductId (already in local)
3299+
};
3300+
var combinedProducts =
3301+
localProducts.UnionBy(
3302+
warehouseProducts,
3303+
product => product.ProductId
3304+
);
3305+
3306+
foreach (var product in combinedProducts)
3307+
{
3308+
Console.WriteLine($"{product.ProductId}: {product.Name} - ${product.Price}");
3309+
}
3310+
3311+
/*
3312+
This code produces the following output:
3313+
3314+
101: Laptop - $1000
3315+
102: Mouse - $100
3316+
103: Keyboard - $120
3317+
104: Monitor - $800
3318+
*/
3319+
}
3320+
// </Snippet207>
3321+
3322+
// <Snippet208>
3323+
public static void UnionByComparerExample()
3324+
{
3325+
(string Email, string FullName)[] marketingList =
3326+
{
3327+
("Mahmoud.Doe@example.com", "Mahmoud Doe"),
3328+
("alice.smith@example.com", "Alice Smith")
3329+
};
3330+
3331+
(string Email, string FullName)[] salesList =
3332+
{
3333+
("ALICE.SMITH@EXAMPLE.COM", "Alice S."), // Duplicate email, different casing
3334+
("Sara.jones@example.com", "Sara Jones")
3335+
};
3336+
3337+
var combinedList =
3338+
marketingList.UnionBy(
3339+
salesList,
3340+
contact => contact.Email,
3341+
StringComparer.OrdinalIgnoreCase
3342+
);
3343+
3344+
foreach (var contact in combinedList)
3345+
{
3346+
Console.WriteLine($"{contact.FullName} ({contact.Email})");
3347+
}
3348+
3349+
/*
3350+
This code produces the following output:
3351+
3352+
Mahmoud Doe (Mahmoud.Doe@example.com)
3353+
Alice Smith (alice.smith@example.com)
3354+
Sara Jones (Sara.jones@example.com)
3355+
*/
3356+
}
3357+
// </Snippet208>
3358+
}
3359+
#endregion
32803360
}
32813361
}

xml/System.Linq/Enumerable.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17924,6 +17924,12 @@ The default equality comparer, <xref:System.Collections.Generic.EqualityComparer
1792417924

1792517925
When the object returned by this method is enumerated, <xref:System.Linq.Enumerable.UnionBy*> enumerates `first` and `second` in that order and yields each element that has not already been yielded.
1792617926

17927+
## Examples
17928+
17929+
The following example demonstrates how to use `UnionBy` to merge two collections of objects while excluding duplicates based on a specific property.
17930+
17931+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" id="Snippet207":::
17932+
1792717933
]]></format>
1792817934
</remarks>
1792917935
<related type="Article" href="/dotnet/csharp/programming-guide/concepts/linq/set-operations?branch=main#union-and-unionby">Union and UnionBy examples</related>
@@ -18008,6 +18014,12 @@ If `comparer` is `null`, the default equality comparer, <xref:System.Collections
1800818014

1800918015
When the object returned by this method is enumerated, <xref:System.Linq.Enumerable.UnionBy*> enumerates `first` and `second` in that order and yields each element that has not already been yielded.
1801018016

18017+
## Examples
18018+
18019+
The following example demonstrates how to use `UnionBy` to merge two collections while using a custom comparer to ignore case sensitivity when checking for duplicate keys.
18020+
18021+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" id="Snippet208":::
18022+
1801118023
]]></format>
1801218024
</remarks>
1801318025
<related type="Article" href="/dotnet/csharp/programming-guide/concepts/linq/set-operations?branch=main#union-and-unionby">Union and UnionBy examples</related>

0 commit comments

Comments
 (0)