C#集合List去掉重复对象的方法

.NET[C#]使用LINQ从List集合中删除重复对象元素(去重)的方法有哪些?

问题描述
比如有如下的List集合:

1 Item1 IT00001 $100
2 Item2 IT00002 $200
3 Item3 IT00003 $150
1 Item1 IT00001 $100
3 Item3 IT00003 $150
使用LINQ如何实现对以上List集合的去重操作,具体实现有哪些呢?

方案一
var distinctItems = items.Distinct();
如果需要对泛型实体中的部分属性进行去重操作,则可以创建一个自定义的比较器:

class DistinctItemComparer : IEqualityComparer {

public bool Equals(Item x, Item y) {
    return x.Id == y.Id &&
        x.Name == y.Name &&
        x.Code == y.Code &&
        x.Price == y.Price;
}

public int GetHashCode(Item obj) {
    return obj.Id.GetHashCode() ^
        obj.Name.GetHashCode() ^
        obj.Code.GetHashCode() ^
        obj.Price.GetHashCode();
}

}
调用方法:

var distinctItems = items.Distinct(new DistinctItemComparer());
方案二(推荐)
var distinctItems = items.GroupBy(x => x.Id).Select(y => y.First());
方案三
使用 MoreLinq 组件:

var distinct = items.DistinctBy( i => i.Id );
方案四
var query = collection.GroupBy(x => x.title).Select(y => y.FirstOrDefault());
方案五
创建静态扩展方法,如:

public static class DistinctHelper
{
public static IEnumerable DistinctBy<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)
{
var identifiedKeys = new HashSet();
return source.Where(element => identifiedKeys.Add(keySelector(element)));
}
}
调用方法:

var outputList = sourceList.DistinctBy(x => x.TargetProperty);
其中 x.TargetProperty 请替换成类对应的属性。

示例程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var people = new List
{
new Person {Id=1,Name=“Curry”,Age=26 },
new Person {Id=1,Name=“Curry”,Age=26 },
new Person {Id=3,Name=“James”,Age=27 },
new Person {Id=4,Name=“Kobe”,Age=38 }
};
var distinctPeople = people.DistinctBy(x => x.Name).ToList();
distinctPeople.ForEach(x =>
Console.WriteLine($“Id:{x.Id},Name:{x.Name},Age:{x.Age}”)
);
Console.ReadKey();
}
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

public static class DistinctHelper
{
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        var identifiedKeys = new HashSet<TKey>();
        return source.Where(element => identifiedKeys.Add(keySelector(element)));
    }
}

}
方案六
public static class DistinctHelper
{
public static IEnumerable DistinctBy<TSource, TKey>(this IEnumerable source, Func<TSource, TKey> keySelector)
{
var identifiedKeys = new HashSet();

        foreach (var item in source)
        {
            if (identifiedKeys.Add(keySelector(item)))
                yield return item;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值