日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

【C#】list 去重(转载)

發(fā)布時間:2024/9/5 C# 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C#】list 去重(转载) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

Enumerable.Distinct 方法?是常用的LINQ擴展方法,屬于System.Linq的Enumerable方法,可用于去除數(shù)組、集合中的重復(fù)元素,還可以自定義去重的規(guī)則。

有兩個重載方法:

//// 摘要: // 通過使用默認的相等比較器對值進行比較返回序列中的非重復(fù)元素。//// 參數(shù): // source:// 要從中移除重復(fù)元素的序列。//// 類型參數(shù): // TSource:// source 中的元素的類型。//// 返回結(jié)果: // 一個 System.Collections.Generic.IEnumerable<T>,包含源序列中的非重復(fù)元素。//// 異常: // System.ArgumentNullException:// source 為 null。public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source);//// 摘要: // 通過使用指定的 System.Collections.Generic.IEqualityComparer<T> 對值進行比較返回序列中的非重復(fù)元素。//// 參數(shù): // source:// 要從中移除重復(fù)元素的序列。//// comparer:// 用于比較值的 System.Collections.Generic.IEqualityComparer<T>。//// 類型參數(shù): // TSource:// source 中的元素的類型。//// 返回結(jié)果: // 一個 System.Collections.Generic.IEnumerable<T>,包含源序列中的非重復(fù)元素。//// 異常: // System.ArgumentNullException:// source 為 null。public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);

第一個方法不帶參數(shù),第二個方法需要傳一個System.Collections.Generic.IEqualityComparer<T>的實現(xiàn)對象

1.值類型元素集合去重

List<int> list = new List<int> { 1, 1, 2, 2, 3, 4, 5, 5 }; list.Distinct().ToList().ForEach(s => Console.WriteLine(s));

執(zhí)行結(jié)果是:1 2 3 4 5

2.引用類型元素集合去重

首先自定義一個Student類

public class Student{public string Name { get; private set; }public int Id { get; private set; }public string Hobby { get; private set; }public Student(string name, int id, string hobby){this.Name = name;this.Id = id;this.Hobby = hobby;}/// <summary>/// 方便輸出,重寫ToString方法/// </summary>/// <returns></returns>public override string ToString(){return string.Format("{0}\t{1}\t{2}", this.Name, this.Id, this.Hobby);}}

使用不到參數(shù)的Distinct方法去重

List<Student> list = new List<Student>() { new Student("James",1,"Basketball"),new Student("James",1,"Basketball"),new Student("Kobe",2,"Basketball"),new Student("Curry",3,"Football"),new Student("Curry",3,"Yoga")};list.Distinct().ToList().ForEach(s => Console.WriteLine(s.ToString()));

執(zhí)行結(jié)果:

可見,并沒有去除重復(fù)的記錄。

不帶comparer參數(shù)的Distinct方法是使用的IEqualityComparer接口的默認比較器進行比較的,對于引用類型,默認比較器比較的是其引用地址,程序中集合里的每一個元素都是個新的實例,引用地址都是不同的,所以不會被作為重復(fù)記錄刪除掉。

因此,我們考慮使用第二個重載方法。

新建一個類,實現(xiàn)IEqualityComparer接口。注意GetHashCode方法的實現(xiàn),只有HashCode相同才會去比較

public class Compare:IEqualityComparer<Student>{public bool Equals(Student x,Student y){return x.Id == y.Id;//可以自定義去重規(guī)則,此處將Id相同的就作為重復(fù)記錄,不管學(xué)生的愛好是什么}public int GetHashCode(Student obj){return obj.Id.GetHashCode();}}

然后調(diào)用

list.Distinct(new Compare()).ToList().ForEach(s => Console.WriteLine(s.ToString()));

執(zhí)行結(jié)果:

我們按照Id去給這個集合去重成功!

3.如何編寫一個具有擴展性的去重方法

public class Compare<T, C> : IEqualityComparer<T>{private Func<T, C> _getField;public Compare(Func<T, C> getfield){this._getField = getfield;}public bool Equals(T x, T y){return EqualityComparer<C>.Default.Equals(_getField(x), _getField(y));}public int GetHashCode(T obj){return EqualityComparer<C>.Default.GetHashCode(this._getField(obj));}}public static class CommonHelper{/// <summary>/// 自定義Distinct擴展方法/// </summary>/// <typeparam name="T">要去重的對象類</typeparam>/// <typeparam name="C">自定義去重的字段類型</typeparam>/// <param name="source">要去重的對象</param>/// <param name="getfield">獲取自定義去重字段的委托</param>/// <returns></returns>public static IEnumerable<T> MyDistinct<T, C>(this IEnumerable<T> source, Func<T, C> getfield){return source.Distinct(new Compare<T, C>(getfield));}}

調(diào)用:

list.MyDistinct(s=>s.Id).ToList().ForEach(s => Console.WriteLine(s.ToString()));

用到了泛型、委托、擴展方法等知識點。可以用于任何集合的各種去重場景

?

轉(zhuǎn)載來源:https://www.cnblogs.com/Robert-go-go/p/5399198.html

轉(zhuǎn)載于:https://www.cnblogs.com/hao-1234-1234/p/8855218.html

總結(jié)

以上是生活随笔為你收集整理的【C#】list 去重(转载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。