List实现某列去重的解决方案
public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
{
private Func<T, V> keySelector;
public CommonEqualityComparer(Func<T, V> keySelector)
{
this.keySelector = keySelector;
}
public bool Equals(T x, T y)
{
return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
}
public int GetHashCode(T obj)
{
return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
}
}
/// <summary>
/// List去重复
/// </summary>
public static class DistinctExtensions
{
public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
{
return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
}
}
调用示例:
result = result.Distinct(p => p.groupid).OrderByDescending(m=>m.addtime).ToList();
作者:天使不哭
微信号:hgmyzhl
微信公众号:小明互联网技术分享社区
CSDN:IT技术分享社区
知乎:IT技术分享社区
出处:小明互联网技术分享社区
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.