Muokkaa

Jaa


Enumerable.UnionBy Method

Definition

Overloads

Name Description
UnionBy<TSource,TKey>(IEnumerable<TSource>, IEnumerable<TSource>, Func<TSource,TKey>)

Produces the set union of two sequences according to a specified key selector function.

UnionBy<TSource,TKey>(IEnumerable<TSource>, IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

Produces the set union of two sequences according to a specified key selector function.

UnionBy<TSource,TKey>(IEnumerable<TSource>, IEnumerable<TSource>, Func<TSource,TKey>)

Source:
Union.cs
Source:
Union.cs
Source:
Union.cs
Source:
Union.cs
Source:
Union.cs

Produces the set union of two sequences according to a specified key selector function.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ UnionBy(System::Collections::Generic::IEnumerable<TSource> ^ first, System::Collections::Generic::IEnumerable<TSource> ^ second, Func<TSource, TKey> ^ keySelector);
public static System.Collections.Generic.IEnumerable<TSource> UnionBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second, Func<TSource,TKey> keySelector);
static member UnionBy : seq<'Source> * seq<'Source> * Func<'Source, 'Key> -> seq<'Source>
<Extension()>
Public Function UnionBy(Of TSource, TKey) (first As IEnumerable(Of TSource), second As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IEnumerable(Of TSource)

Type Parameters

TSource

The type of the elements of the input sequences.

TKey

The type of key to identify elements by.

Parameters

first
IEnumerable<TSource>

An IEnumerable<T> whose distinct elements form the first set for the union.

second
IEnumerable<TSource>

An IEnumerable<T> whose distinct elements form the second set for the union.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

Returns

IEnumerable<TSource>

An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.

Exceptions

first or second is null.

Examples

The following example demonstrates how to use UnionBy to merge two collections of objects while excluding duplicates based on a specific property.

public static void UnionByKeySelectorExample()
{
    (int ProductId, string Name , decimal Price)[] localProducts =
    {
        (101, "Laptop", 1000m),                 
        (102, "Mouse", 100m),
        (103, "Keyboard", 120m)
    };

    (int ProductId, string Name, decimal Price)[] warehouseProducts =
    {
        (102, "Mouse", 100m),      // Duplicate ProductId (already in local)
        (104, "Monitor", 800m),
        (101, "Laptop", 1000m)     // Duplicate ProductId (already in local)
    };
    var combinedProducts =
        localProducts.UnionBy(
            warehouseProducts,
            product => product.ProductId
        );

    foreach (var product in combinedProducts)
    {
        Console.WriteLine($"{product.ProductId}: {product.Name} - ${product.Price}");
    }

    /*
    This code produces the following output:

    101: Laptop - $1000
    102: Mouse - $100
    103: Keyboard - $120
    104: Monitor - $800
    */
}

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

The default equality comparer, Default, is used to compare values.

When the object returned by this method is enumerated, UnionBy enumerates first and second in that order and yields each element that has not already been yielded.

See also

Applies to

UnionBy<TSource,TKey>(IEnumerable<TSource>, IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

Source:
Union.cs
Source:
Union.cs
Source:
Union.cs
Source:
Union.cs
Source:
Union.cs

Produces the set union of two sequences according to a specified key selector function.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ UnionBy(System::Collections::Generic::IEnumerable<TSource> ^ first, System::Collections::Generic::IEnumerable<TSource> ^ second, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<TSource> UnionBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member UnionBy : seq<'Source> * seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Source>
<Extension()>
Public Function UnionBy(Of TSource, TKey) (first As IEnumerable(Of TSource), second As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of TSource)

Type Parameters

TSource

The type of the elements of the input sequences.

TKey

The type of key to identify elements by.

Parameters

first
IEnumerable<TSource>

An IEnumerable<T> whose distinct elements form the first set for the union.

second
IEnumerable<TSource>

An IEnumerable<T> whose distinct elements form the second set for the union.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

The IEqualityComparer<T> to compare values.

Returns

IEnumerable<TSource>

An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.

Exceptions

first or second is null.

Examples

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.

public static void UnionByComparerExample()
{
    (string Email, string FullName)[] marketingList =
    {
        ("Mahmoud.Doe@example.com", "Mahmoud Doe"),
        ("alice.smith@example.com", "Alice Smith")
    };

    (string Email, string FullName)[] salesList =
    {
        ("ALICE.SMITH@EXAMPLE.COM", "Alice S."), // Duplicate email, different casing
        ("Sara.jones@example.com", "Sara Jones")
    };

    var combinedList =
        marketingList.UnionBy(
            salesList,
            contact => contact.Email,
            StringComparer.OrdinalIgnoreCase
        );

    foreach (var contact in combinedList)
    {
        Console.WriteLine($"{contact.FullName} ({contact.Email})");
    }

    /*
    This code produces the following output:

    Mahmoud Doe (Mahmoud.Doe@example.com)
    Alice Smith (alice.smith@example.com)
    Sara Jones (Sara.jones@example.com)
    */
}

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

If comparer is null, the default equality comparer, Default, is used to compare values.

When the object returned by this method is enumerated, UnionBy enumerates first and second in that order and yields each element that has not already been yielded.

See also

Applies to