Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InlineComparer<T> : IEqualityComparer<T>
- {
- private readonly Func<T, T, bool> getEquals;
- private readonly Func<T, int> getHashCode;
- public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
- {
- getEquals = equals;
- getHashCode = hashCode;
- }
- public bool Equals(T x, T y)
- {
- return getEquals(x, y);
- }
- public int GetHashCode(T obj)
- {
- return getHashCode(obj);
- }
- }
- var formatIssues = issues.Where(i => i.IsFormatError == true);
- var groupIssues = issues.Where(i => i.IsGroupError == true);
- var dupComparer = new InlineComparer<Issue>((i1, i2) => i1.ColumnInfo.ColumnIndex == i2.ColumnInfo.ColumnIndex,
- i => i.ColumnInfo.ColumnIndex);
- var filteredIssues = groupIssues.Union(formatIssues, dupComparer);
- public class InlineComparer<T> : IEqualityComparer<T>
- {
- private readonly Func<T, T, bool> getEquals;
- private readonly Func<T, int> getHashCode;
- public InlineComparer(Func<T, T, bool> equals, Func<T, int> hashCode)
- {
- getEquals = equals;
- getHashCode = hashCode;
- }
- public bool Equals(T x, T y)
- {
- return getEquals(x, y);
- }
- public int GetHashCode(T obj)
- {
- return getHashCode(obj);
- }
- }
- class TestClass
- {
- public string S { get; set; }
- }
- [TestMethod]
- public void testThis()
- {
- var l1 = new List<TestClass>()
- {
- new TestClass() {S = "one"},
- new TestClass() {S = "two"},
- };
- var l2 = new List<TestClass>()
- {
- new TestClass() {S = "three"},
- new TestClass() {S = "two"},
- };
- var dupComparer = new InlineComparer<TestClass>((i1, i2) => i1.S == i2.S, i => i.S.GetHashCode());
- var unionList = l1.Union(l2, dupComparer);
- Assert.AreEqual(3, unionList);
- }
- formatIssues.Union(groupIssues).DistinctBy(x => x.ColumnIndex)
- public static IEnumerable<TSource> DistinctBy<TSource, TKey>
- (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
- {
- HashSet<TKey> knownKeys = new HashSet<TKey>();
- foreach (TSource element in source)
- {
- if (knownKeys.Add(keySelector(element)))
- {
- yield return element;
- }
- }
- }
- var formatIssues = issues.Where(i => i.IsFormatError == true);
- var groupIssues = issues.Where(i => i.IsGroupError == true);
- var dupeIssues = issues.Where(i => issues.Except(new List<Issue> {i})
- .Any(x => x.ColumnIndex == i.ColumnIndex));
- var filteredIssues = formatIssues.Union(groupIssues).Except(dupeIssues);
Add Comment
Please, Sign In to add comment