// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; namespace System.Linq { public static partial class EnumerableEx { /// /// Returns a sequence from a dictionary based on the result of evaluating a selector function. /// /// Type of the selector value. /// Result sequence element type. /// Selector function used to pick a sequence from the given sources. /// Dictionary mapping selector values onto resulting sequences. /// The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence. public static IEnumerable Case(Func selector, IDictionary> sources) { if (selector == null) throw new ArgumentNullException(nameof(selector)); if (sources == null) throw new ArgumentNullException(nameof(sources)); return Case(selector, sources, Enumerable.Empty()); } /// /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a /// default sequence. /// /// Type of the selector value. /// Result sequence element type. /// Selector function used to pick a sequence from the given sources. /// Dictionary mapping selector values onto resulting sequences. /// /// Default sequence to return in case there's no corresponding source for the computed /// selector value. /// /// The source sequence corresponding with the evaluated selector value; otherwise, the default source. public static IEnumerable Case(Func selector, IDictionary> sources, IEnumerable defaultSource) { if (selector == null) throw new ArgumentNullException(nameof(selector)); if (sources == null) throw new ArgumentNullException(nameof(sources)); if (defaultSource == null) throw new ArgumentNullException(nameof(defaultSource)); return Defer(() => { if (!sources.TryGetValue(selector(), out var result)) { result = defaultSource; } return result; }); } } }