码迷,mamicode.com
首页 > Windows程序 > 详细

C# 按条件去重

时间:2020-05-12 09:55:51      阅读:118      评论:0      收藏:0      [点我收藏+]

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

添加这段代码即可。

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# 按条件去重

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!