Para cualquiera que todavía parece; Aquí hay otra forma de implementar un comparador lambda personalizado.
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;
}
}
a continuación, puede crear una extensión para el linq distinto que puede tener en
public static IEnumerable<T> Distinct<T>(this IEnumerable<T> list, Func<T, T, bool> lambda)
{
return list.Distinct(new LambdaComparer<T>(lambda));
}
uso de lambda:
var availableItems = list.Distinct((p, p1) => p.Id== p1.Id);
Oh tan por "tipo más grande" que puede significar que Todavía quiero todas las propiedades en el resultado, aunque solo quiero comparar algunas propiedades para determinar la distinción. –
@TheRedPea: Sí, exactamente. –