Guest User

Untitled

a guest
Jan 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. public class InlineComparer<T> : IEqualityComparer<T>
  2. {
  3. private readonly Func<T, T, bool> getEquals;
  4. private readonly Func<T, int> getHashCode;
  5.  
  6. public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
  7. {
  8. getEquals = equals;
  9. getHashCode = hashCode;
  10. }
  11.  
  12. public bool Equals(T x, T y)
  13. {
  14. return getEquals(x, y);
  15. }
  16.  
  17. public int GetHashCode(T obj)
  18. {
  19. return getHashCode(obj);
  20. }
  21. }
  22.  
  23. var formatIssues = issues.Where(i => i.IsFormatError == true);
  24. var groupIssues = issues.Where(i => i.IsGroupError == true);
  25.  
  26. var dupComparer = new InlineComparer<Issue>((i1, i2) => i1.ColumnInfo.ColumnIndex == i2.ColumnInfo.ColumnIndex,
  27. i => i.ColumnInfo.ColumnIndex);
  28.  
  29. var filteredIssues = groupIssues.Union(formatIssues, dupComparer);
  30.  
  31. public class InlineComparer<T> : IEqualityComparer<T>
  32. {
  33. private readonly Func<T, T, bool> getEquals;
  34. private readonly Func<T, int> getHashCode;
  35.  
  36. public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
  37. {
  38. getEquals = equals;
  39. getHashCode = hashCode;
  40. }
  41.  
  42. public bool Equals(T x, T y)
  43. {
  44. return getEquals(x, y);
  45. }
  46.  
  47. public int GetHashCode(T obj)
  48. {
  49. return getHashCode(obj);
  50. }
  51. }
  52.  
  53. class TestClass
  54. {
  55. public string S { get; set; }
  56. }
  57.  
  58. [TestMethod]
  59. public void testThis()
  60. {
  61. var l1 = new List<TestClass>()
  62. {
  63. new TestClass() {S = "one"},
  64. new TestClass() {S = "two"},
  65. };
  66. var l2 = new List<TestClass>()
  67. {
  68. new TestClass() {S = "three"},
  69. new TestClass() {S = "two"},
  70. };
  71.  
  72. var dupComparer = new InlineComparer<TestClass>((i1, i2) => i1.S == i2.S, i => i.S.GetHashCode());
  73.  
  74. var unionList = l1.Union(l2, dupComparer);
  75.  
  76. Assert.AreEqual(3, unionList);
  77. }
  78.  
  79. formatIssues.Union(groupIssues).DistinctBy(x => x.ColumnIndex)
  80.  
  81. public static IEnumerable<TSource> DistinctBy<TSource, TKey>
  82. (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  83. {
  84. HashSet<TKey> knownKeys = new HashSet<TKey>();
  85. foreach (TSource element in source)
  86. {
  87. if (knownKeys.Add(keySelector(element)))
  88. {
  89. yield return element;
  90. }
  91. }
  92. }
  93.  
  94. var formatIssues = issues.Where(i => i.IsFormatError == true);
  95. var groupIssues = issues.Where(i => i.IsGroupError == true);
  96.  
  97. var dupeIssues = issues.Where(i => issues.Except(new List<Issue> {i})
  98. .Any(x => x.ColumnIndex == i.ColumnIndex));
  99.  
  100. var filteredIssues = formatIssues.Union(groupIssues).Except(dupeIssues);
Add Comment
Please, Sign In to add comment