C# 按条件去重

2021-03-06 04:26

阅读:559

标签:ash   common   sharp   条件   ext   ret   arp   source   sele   

添加这段代码即可。

public class CommonEqualityComparer : IEqualityComparer
???{
??????private Func keySelector;

??????public CommonEqualityComparer(Func keySelector)
??????{
?????????this.keySelector = keySelector;
??????}

??????public bool Equals(T x, T y)
??????{
?????????return EqualityComparer.Default.Equals(keySelector(x), keySelector(y));
??????}

??????public int GetHashCode(T obj)
??????{
?????????return EqualityComparer.Default.GetHashCode(keySelector(obj));
??????}
???}

???public static class DistinctExtensions
???{
??????public static IEnumerable Distinct(this IEnumerable source, Func keySelector)
??????{
?????????return source.Distinct(new CommonEqualityComparer(keySelector));
??????}
???}

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

C# 按条件去重

标签:ash   common   sharp   条件   ext   ret   arp   source   sele   

原文地址:https://www.cnblogs.com/chendeqiang/p/12861597.html

上一篇:C#容器

下一篇:C# 异步编程


评论


亲,登录后才可以留言!