C# 按条件去重

添加这段代码即可。

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));
??????}
???}

???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));
??????}
???}

参考链接:
https://cloud.tencent.com/developer/article/1344620

C# 按条件去重

上一篇:C# 数据操作系列 - 3. ADO.NET 离线查询


下一篇:安装纯净的微软官方win10系统,操作简单!不纯净可以来打我!