EnumerableExt.cs 3.6 KB

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