Method ExceptBy
| Edit this page View SourceExceptBy<TSource, TKey>(IEnumerable<TSource>, IEnumerable<TSource>, Func<TSource, TKey>)
Returns the set of elements in the first sequence which aren't in the second sequence, according to a given key selector.
Declaration
public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TKey> keySelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | first | The sequence of potentially included elements. |
IEnumerable<TSource> | second | The sequence of elements whose keys may prevent elements in |
Func<TSource, TKey> | keySelector | The mapping from source element to key. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence of elements from |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the input sequences. |
TKey | The type of the key returned by |
Remarks
This is a set operation; if multiple elements in first
have equal keys, only the first
such element is returned. This operator uses deferred execution and streams the results, although a set of
keys from second
is immediately selected and retained.
Examples
The following code example demonstrates how to filter items from a sequence using a second sequence using ExceptBy
.
var sequence = new[] { "aaa", "bb", "c", "dddd", };
// Determine if sequence ends with the ends sequence
var result = sequence.ExceptBy(new[] { "a", "b", }, s => s[0]);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [c, dddd]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ExceptBy<TSource, TKey>(IEnumerable<TSource>, IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)
Returns the set of elements in the first sequence which aren't in the second sequence, according to a given key selector.
Declaration
public static IEnumerable<TSource> ExceptBy<TSource, TKey>(this IEnumerable<TSource> first, IEnumerable<TSource> second, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? keyComparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | first | The sequence of potentially included elements. |
IEnumerable<TSource> | second | The sequence of elements whose keys may prevent elements in |
Func<TSource, TKey> | keySelector | The mapping from source element to key. |
IEqualityComparer<TKey> | keyComparer | The equality comparer to use to determine whether or not keys are equal. If null, the default equality
comparer for |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | A sequence of elements from |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements in the input sequences. |
TKey | The type of the key returned by |
Remarks
This is a set operation; if multiple elements in first
have equal keys, only the first
such element is returned. This operator uses deferred execution and streams the results, although a set of
keys from second
is immediately selected and retained.
Examples
The following code example demonstrates how to filter items from a sequence using a second sequence using ExceptBy
.
var sequence = new[]
{
(key: "aaa", value: 1),
(key: "bb", value: 2),
(key: "c", value: 3),
(key: "dddd", value: 4),
};
// Determine if sequence ends with the ends sequence
var result = sequence
.ExceptBy(
new[] { (key: "A", value: 13), (key: "D", value: 14), },
s => s.key[..1],
StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(bb, 2), (c, 3)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|