Enumerable.UnionBy Method

Definition

Overloads

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>)

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.

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>)

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.

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