Case.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Returns a sequence from a dictionary based on the result of evaluating a selector function.
  11. /// </summary>
  12. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  13. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  14. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  15. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  16. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>
  17. public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
  18. {
  19. if (selector == null)
  20. {
  21. throw new ArgumentNullException(nameof(selector));
  22. }
  23. if (sources == null)
  24. {
  25. throw new ArgumentNullException(nameof(sources));
  26. }
  27. return Case(selector, sources, Enumerable.Empty<TResult>());
  28. }
  29. /// <summary>
  30. /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a
  31. /// default sequence.
  32. /// </summary>
  33. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  34. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  35. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  36. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  37. /// <param name="defaultSource">
  38. /// Default sequence to return in case there's no corresponding source for the computed
  39. /// selector value.
  40. /// </param>
  41. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>
  42. public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
  43. {
  44. if (selector == null)
  45. {
  46. throw new ArgumentNullException(nameof(selector));
  47. }
  48. if (sources == null)
  49. {
  50. throw new ArgumentNullException(nameof(sources));
  51. }
  52. if (defaultSource == null)
  53. {
  54. throw new ArgumentNullException(nameof(defaultSource));
  55. }
  56. return Defer(() =>
  57. {
  58. if (!sources.TryGetValue(selector(), out var result))
  59. {
  60. result = defaultSource;
  61. }
  62. return result;
  63. });
  64. }
  65. }
  66. }