C# Linq 的三种去重方式(Distinct)
前言
首先给出我们需要用到的对象,如下:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
接下来我们添加100万条数据到集合中,如下:
var list = new List<Person>();
for (int i = 0; i < 1000000; i++)
{
list.Add(new Person() { Age = 18, Name = "迷恋自留地" });
}
for (int i = 0; i < 1000; i++)
{
list.Add(new Person() { Age = 19, Name = "迷恋自留地" });
}
第一种分组去重
年龄和名称进行分组,然后取第一条即可达到去重,如下:
var list1 = list.GroupBy(d => new { d.Age, d.Name })
.Select(d => d.FirstOrDefault())
.ToList();
第二种 HashSet去重 (扩展方法)
public static IEnumerable<TSource> Distinct<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
{
var hashSet = new HashSet<TKey>();
foreach (TSource element in source)
{
if (hashSet.Add(keySelector(element)))
{
yield return element;
}
}
}
述扩展方法即可去重,如下:
var list2 = list.Distinct(d => new { d.Age, d.Name }).ToList();
第三种 IEqualityComparer去重 (扩展方法)
public static class Extensions
{
public static IEnumerable<T> Distinct<T>(
this IEnumerable<T> source, Func<T, T, bool> comparer)
where T : class
=> source.Distinct(new DynamicEqualityComparer<T>(comparer));
private sealed class DynamicEqualityComparer<T> : IEqualityComparer<T>
where T : class
{
private readonly Func<T, T, bool> _func;
public DynamicEqualityComparer(Func<T, T, bool> func)
{
_func = func;
}
public bool Equals(T x, T y) => _func(x, y);
public int GetHashCode(T obj) => 0;
}
}
最终通过指定属性进行比较即可去重,如下:
list = list.Distinct((a, b) => a.Age == b.Age && a.Name == b.Name).ToList();
性能比较
我们来分析其耗时情况,如下:
var list = new List<Person>();
for (int i = 0; i < 1000000; i++)
{
list.Add(new Person() { Age = 18, Name = "jeffcky" });
}
var time1 = Time(() =>
{
list.GroupBy(d => new { d.Age, d.Name })
.Select(d => d.FirstOrDefault())
.ToList();
});
Console.WriteLine($"分组耗时:{time1}");
var time2 = Time(() =>
{
list.Distinct(d => new { d.Age, d.Name }).ToList();
});
Console.WriteLine($"HashSet耗时:{time2}");
var time3 = Time(() =>
{
list.Distinct((a, b) => a.Age == b.Age && a.Name == b.Name).ToList();
});
Console.WriteLine($"委托耗时:{time3}");
static long Time(Action action)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
action();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}

转自:https://www.modb.pro/db/104719
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示