I want to write a new override of Except extension method for IEnumerable which is able to take a comparer inline instead of using IEqualityComparer.
A, B are collections of a reference type..
Something like this:
A: [1, A], [2, A], [3, A]
B: [3, B], [4, B], [5, B]
C = A.Except(B, (a,b) => a.Id == b.Id);
C: [1, A], [2, A]
I wonder if you could help me with the code of the method.
public static class IEnumerableExntesion
{
public IEnumerable<T> Except<T>(this IEnumerable<T> source,
IEnumerable<T> second,
Func<T, T, bool> predicate)
{
}
}
I was thinking of:
return source.Where (s => !second.Any(p => p.Id == s.Id));
But actually I couldn’t convert it to a generic solution using the passed predicate!
Any help!
Do you need to do the comparison using a predicate or can you use a projection instead and compare the projected values? If so then you could use some sort of
ExceptBymethod: