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. Can I use a lambda expression to provide the equality function, like when I use Where or other LINQ functions?
If I can't, is there an alternative?
So performance-wise, there's no difference whatsoever between the two. Which one you should use is mostly personal preference, many people prefer lambda expressions because they're shorter and more concise, but personally I prefer the query syntax having worked extensively with SQL.
The => operator can be used in two ways in C#: As the lambda operator in a lambda expression, it separates the input variables from the lambda body. In an expression body definition, it separates a member name from the member implementation.
For any one still looking; here's another way of implementing a custom lambda comparer.
public class LambdaComparer<T> : IEqualityComparer<T> { private readonly Func<T, T, bool> _expression; public LambdaComparer(Func<T, T, bool> 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
/// <summary> /// Returns all items in the first collection except the ones in the second collection that match the lambda condition /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="listA">The first list</param> /// <param name="listB">The second list</param> /// <param name="lambda">The filter expression</param> /// <returns>The filtered list</returns> public static IEnumerable<T> Except<T>(this IEnumerable<T> listA, IEnumerable<T> listB, Func<T, T, bool> lambda) { return listA.Except(listB, new LambdaComparer<T>(lambda)); } /// <summary> /// Returns all items in the first collection that intersect the ones in the second collection that match the lambda condition /// </summary> /// <typeparam name="T">The type</typeparam> /// <param name="listA">The first list</param> /// <param name="listB">The second list</param> /// <param name="lambda">The filter expression</param> /// <returns>The filtered list</returns> public static IEnumerable<T> Intersect<T>(this IEnumerable<T> listA, IEnumerable<T> listB, Func<T, T, bool> lambda) { return listA.Intersect(listB, new LambdaComparer<T>(lambda)); }
Usage:
var availableItems = allItems.Except(filterItems, (p, p1) => p.Id== p1.Id);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With