.NET[C#]使用LINQ從List 集合中刪除重復對象元素(去重)的方法有哪些?


問題描述

使用LINQ如何實現對以上List集合的去

比如有如下的List集合:

1         Item1       IT00001        $100
2         Item2       IT00002        $200
3         Item3       IT00003        $150
1         Item1       IT00001        $100
3         Item3       IT00003        $150

  

重操作,具體實現有哪些呢?

方案一

var distinctItems = items.Distinct();

如果需要對泛型實體中的部分屬性進行去重操作,則可以創建一個自定義的比較器:

class DistinctItemComparer : IEqualityComparer<Item> {

    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<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)));
    }
}

調用方法:

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<Person>
            {
                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<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            var identifiedKeys = new HashSet<TKey>();

            foreach (var item in source)
            {
                if (identifiedKeys.Add(keySelector(item)))
                    yield return item;
            }
        }
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM