I know I can call linq\'s Except and specify a custom IEqualityComparer, but implementing a new Comparer class for each data type seems like an overkill for this purpose. Ca
For any one still looking; here's another way of implementing a custom lambda comparer.
public class LambdaComparer : IEqualityComparer
{
private readonly Func _expression;
public LambdaComparer(Func lambda)
{
_expression = lambda;
}
public bool Equals(T x, T y)
{
return _expression(x, y);
}
public int GetHashCode(T obj)
{
/*
If you just return 0 for the hash the Equals comparer will kick in.
The underlying evaluation checks the hash and then short circuits the evaluation if it is false.
Otherwise, it checks the Equals. If you force the hash to be true (by assuming 0 for both objects),
you will always fall through to the Equals check which is what we are always going for.
*/
return 0;
}
}
you can then create an extension for the linq Except an Intersect that take in lambda's
///
/// Returns all items in the first collection except the ones in the second collection that match the lambda condition
///
/// The type
/// The first list
/// The second list
/// The filter expression
/// The filtered list
public static IEnumerable Except(this IEnumerable listA, IEnumerable listB, Func lambda)
{
return listA.Except(listB, new LambdaComparer(lambda));
}
///
/// Returns all items in the first collection that intersect the ones in the second collection that match the lambda condition
///
/// The type
/// The first list
/// The second list
/// The filter expression
/// The filtered list
public static IEnumerable Intersect(this IEnumerable listA, IEnumerable listB, Func lambda)
{
return listA.Intersect(listB, new LambdaComparer(lambda));
}
Usage:
var availableItems = allItems.Except(filterItems, (p, p1) => p.Id== p1.Id);