Method Case
| Edit this page View SourceCase<TValue, TResult>(Func<TValue>, IDictionary<TValue, IEnumerable<TResult>>)
Returns a sequence from a dictionary based on the result of evaluating a selector function.
Declaration
public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources) where TValue : notnull
Parameters
Type | Name | Description |
---|---|---|
Func<TValue> | selector | Selector function used to pick a sequence from the given sources. |
IDictionary<TValue, IEnumerable<TResult>> | sources | Dictionary mapping selector values onto resulting sequences. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence. |
Type Parameters
Name | Description |
---|---|
TValue | Type of the selector value. |
TResult | Result sequence element type. |
Remarks
selector
is not evaluated until enumeration. The value returned will be used to select a
sequence from sources
; enumeration will continue with items from that sequence.
If the value returned by selector
is not present in sources
, the
resulting sequence will be empty.
Examples
The following code example demonstrates how to select which sequence to return values from, using Case
.
var sequences = Enumerable.Range(1, 5)
.ToDictionary(
x => x,
x => Enumerable.Range(1, x));
// Use a function to select which sequence to return values from.
var selector = 1;
var result = SuperEnumerable
.Case(
() => selector,
sequences);
Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
selector = 4;
Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
selector = 2;
Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
// This code produces the following output:
// Selector: 1; result.Count(): 1.
// Selector: 4; result.Count(): 4.
// Selector: 2; result.Count(): 2.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException | (Thrown lazily) The sequence in |
Case<TValue, TResult>(Func<TValue>, IDictionary<TValue, IEnumerable<TResult>>, IEnumerable<TResult>)
Returns a sequence from a dictionary based on the result of evaluating a selector function.
Declaration
public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource) where TValue : notnull
Parameters
Type | Name | Description |
---|---|---|
Func<TValue> | selector | Selector function used to pick a sequence from the given sources. |
IDictionary<TValue, IEnumerable<TResult>> | sources | Dictionary mapping selector values onto resulting sequences. |
IEnumerable<TResult> | defaultSource | Default sequence to return in case there's no corresponding source for the computed selector value. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | The source sequence corresponding with the evaluated selector value; otherwise, the |
Type Parameters
Name | Description |
---|---|
TValue | Type of the selector value. |
TResult | Result sequence element type. |
Remarks
selector
is not evaluated until enumeration. The value returned will be used to select a
sequence from sources
; enumeration will continue with items from that sequence.
Examples
The following code example demonstrates how to select which sequence to return values from, using Case
.
var sequences = Enumerable.Range(1, 5)
.ToDictionary(
x => x,
x => Enumerable.Range(1, x));
// Use a function to select which sequence to return values from.
var selector = 1;
var result = SuperEnumerable
.Case(
() => selector,
sequences,
Enumerable.Range(1, 100));
Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
selector = 4;
Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
selector = 20;
Console.WriteLine($"Selector: {selector}; result.Count(): {result.Count()}.");
// This code produces the following output:
// Selector: 1; result.Count(): 1.
// Selector: 4; result.Count(): 4.
// Selector: 20; result.Count(): 100.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException | (Thrown lazily) The sequence in |