【C#】list 去重

时间:2022-06-19 03:05:43

Enumerable.Distinct 要领 是常用的LINQ扩展要领,属于System.Linq的Enumerable要领,可用于去除数组、调集中的反复元素,还可以自界说去重的法则。

有两个重载要领:

// // 摘要: // 通过使用默认的相等对照器对值进行对照返回序列中的非反复元素。 // // 参数: // source: // 要从中移除反复元素的序列。 // // 类型参数: // TSource: // source 中的元素的类型。 // // 返回功效: // 一个 System.Collections.Generic.IEnumerable<T>,包罗源序列中的非反复元素。 // // 异常: // System.ArgumentNullException: // source 为 null。 public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source); // // 摘要: // 通过使用指定的 System.Collections.Generic.IEqualityComparer<T> 对值进行对照返回序列中的非反复元素。 // // 参数: // source: // 要从中移除反复元素的序列。 // // comparer: // 用于对照值的 System.Collections.Generic.IEqualityComparer<T>。 // // 类型参数: // TSource: // source 中的元素的类型。 // // 返回功效: // 一个 System.Collections.Generic.IEnumerable<T>,包罗源序列中的非反复元素。 // // 异常: // System.ArgumentNullException: // source 为 null。 public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);

第一个要领不带参数,第二个要领需要传一个System.Collections.Generic.IEqualityComparer<T>的实现东西

1.值类型元素调集去重

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

执行功效是: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); } }

使用不到参数的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()));

执行功效:

可见,并没有去除反复的记录。

不带comparer参数的Distinct要领是使用的IEqualityComparer接口的默认对照器进行对照的,对付引用类型,默认对照器对照的是其引用地点,措施中调集里的每一个元素都是个新的实例,引用地点都是差此外,所以不会被作为反复记录删除去。

因此,我们考虑使用第二个重载要领。

新建一个类,,实现IEqualityComparer接口。注意GetHashCode要领的实现,只有HashCode不异才会去对照

public class Compare:IEqualityComparer<Student> { public bool Equals(Student x,Student y) { return x.Id == y.Id;//可以自界说去重法则,此处将Id不异的就作为反复记录,不管学生的喜好是什么 } public int GetHashCode(Student obj) { return obj.Id.GetHashCode(); } }

然后挪用

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

执行功效:

我们凭据Id去给这个调集去重告成!

3.如何编写一个具有扩展性的去重要领