EnumerableExt.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Quadarax.Foundation.Core.Value.Extensions
  5. {
  6. public static class EnumerableExt
  7. {
  8. private static Random _rand = new Random();
  9. private const string CS_MESSAGE_MUST_HAVE_ONE = "Collection must have at least one element.";
  10. private const string CS_MESSAGE_MUST_POSITIVE = "Must be positive number.";
  11. private const string CS_MESSAGE_MUST_SMALLER = "fromIndex must be smaller or equal to toIndex.";
  12. public static TElement GetRandom<TElement>(this IEnumerable<TElement> owner)
  13. {
  14. if (owner == null)
  15. throw new ArgumentNullException(nameof(owner));
  16. if (!owner.Any())
  17. throw new ArgumentOutOfRangeException(nameof(owner), CS_MESSAGE_MUST_HAVE_ONE);
  18. return GetRandom(owner, 0, owner.Count());
  19. }
  20. public static TElement GetRandom<TElement>(this IEnumerable<TElement> owner, int fromIndex, int toIndex = -1)
  21. {
  22. if (owner == null)
  23. throw new ArgumentNullException(nameof(owner));
  24. if (!owner.Any())
  25. throw new ArgumentOutOfRangeException(nameof(owner), CS_MESSAGE_MUST_HAVE_ONE);
  26. if (toIndex == -1)
  27. toIndex = owner.Count() - 1;
  28. if (fromIndex<0)
  29. throw new ArgumentOutOfRangeException(nameof(fromIndex), CS_MESSAGE_MUST_POSITIVE);
  30. if (toIndex<0)
  31. throw new ArgumentOutOfRangeException(nameof(fromIndex), CS_MESSAGE_MUST_POSITIVE);
  32. if (fromIndex>toIndex)
  33. throw new ArgumentOutOfRangeException(nameof(fromIndex), CS_MESSAGE_MUST_SMALLER);
  34. var index = _rand.Next(fromIndex, toIndex);
  35. return owner.Skip(index).Take(1).First();
  36. }
  37. public static IEnumerable<TElement> CompareDiff<TElement>(this IEnumerable<TElement> owner,
  38. IEnumerable<TElement> other)
  39. {
  40. if (owner== null)
  41. throw new ArgumentNullException(nameof(owner));
  42. if (other == null)
  43. throw new ArgumentNullException(nameof(other));
  44. var ownerArr = (owner as TElement[] ?? owner.ToArray()).ToArray();
  45. var otherArr = (other as TElement[] ?? other.ToArray()).ToArray();
  46. var result = new List<TElement>();
  47. foreach (var elem in ownerArr)
  48. {
  49. if (!otherArr.Contains(elem)) result.Add(elem);
  50. }
  51. foreach (var elem in otherArr)
  52. {
  53. if (!ownerArr.Contains(elem)) result.Add(elem);
  54. }
  55. return result.Distinct();
  56. }
  57. public static IEnumerable<TElement> CompareCommon<TElement>(this IEnumerable<TElement> owner,
  58. IEnumerable<TElement> other)
  59. {
  60. if (owner== null)
  61. throw new ArgumentNullException(nameof(owner));
  62. if (other == null)
  63. throw new ArgumentNullException(nameof(other));
  64. var ownerArr = (owner as TElement[] ?? owner.ToArray()).ToArray();
  65. var otherArr = (other as TElement[] ?? other.ToArray()).ToArray();
  66. var result = new List<TElement>();
  67. foreach (var elem in ownerArr)
  68. {
  69. if (otherArr.Contains(elem)) result.Add(elem);
  70. }
  71. foreach (var elem in otherArr)
  72. {
  73. if (ownerArr.Contains(elem)) result.Add(elem);
  74. }
  75. return result.Distinct();
  76. }
  77. public static IEnumerable<TElement> WhereEquals<TElement>(this IEnumerable<TElement> owner, TElement? value,IEqualityComparer<TElement>? comparer = null)
  78. {
  79. if (owner== null)
  80. throw new ArgumentNullException(nameof(owner));
  81. if (comparer == null)
  82. return owner.Where(x => Equals(x, value));
  83. return owner.Where(x => comparer.Equals(x,value));
  84. }
  85. }
  86. }