Case.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Returns a sequence from a dictionary based on the result of evaluating a selector function.
  14. /// </summary>
  15. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  16. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  17. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  18. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  19. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>
  20. public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
  21. {
  22. if (selector == null)
  23. throw new ArgumentNullException(nameof(selector));
  24. if (sources == null)
  25. throw new ArgumentNullException(nameof(sources));
  26. return Case(selector, sources, Enumerable.Empty<TResult>());
  27. }
  28. /// <summary>
  29. /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a
  30. /// default sequence.
  31. /// </summary>
  32. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  33. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  34. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  35. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  36. /// <param name="defaultSource">
  37. /// Default sequence to return in case there's no corresponding source for the computed
  38. /// selector value.
  39. /// </param>
  40. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>
  41. public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
  42. {
  43. if (selector == null)
  44. throw new ArgumentNullException(nameof(selector));
  45. if (sources == null)
  46. throw new ArgumentNullException(nameof(sources));
  47. if (defaultSource == null)
  48. throw new ArgumentNullException(nameof(defaultSource));
  49. return Defer(() =>
  50. {
  51. if (!sources.TryGetValue(selector(), out var result))
  52. result = defaultSource;
  53. return result;
  54. });
  55. }
  56. }
  57. }