Remove duplicates in the list using linq

前端 未结 11 1344
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 03:04

I have a class Items with properties (Id, Name, Code, Price).

The List of Items is populated with duplicated items.

F

相关标签:
11条回答
  • 2020-11-22 03:26
    List<Employee> employees = new List<Employee>()
    {
        new Employee{Id =1,Name="AAAAA"}
        , new Employee{Id =2,Name="BBBBB"}
        , new Employee{Id =3,Name="AAAAA"}
        , new Employee{Id =4,Name="CCCCC"}
        , new Employee{Id =5,Name="AAAAA"}
    };
    
    List<Employee> duplicateEmployees = employees.Except(employees.GroupBy(i => i.Name)
                                                 .Select(ss => ss.FirstOrDefault()))
                                                .ToList();
    
    0 讨论(0)
  • 2020-11-22 03:31

    If there is something that is throwing off your Distinct query, you might want to look at MoreLinq and use the DistinctBy operator and select distinct objects by id.

    var distinct = items.DistinctBy( i => i.Id );
    
    0 讨论(0)
  • 2020-11-22 03:33

    Try this extension method out. Hopefully this could help.

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

    Usage:

    var outputList = sourceList.DistinctBy(x => x.TargetProperty);
    
    0 讨论(0)
  • 2020-11-22 03:33

    Another workaround, not beautiful buy workable.

    I have an XML file with an element called "MEMDES" with two attribute as "GRADE" and "SPD" to record the RAM module information. There are lot of dupelicate items in SPD.

    So here is the code I use to remove the dupelicated items:

            IEnumerable<XElement> MList =
                from RAMList in PREF.Descendants("MEMDES")
                where (string)RAMList.Attribute("GRADE") == "DDR4"
                select RAMList;
    
            List<string> sellist = new List<string>();
    
            foreach (var MEMList in MList)
            {
                sellist.Add((string)MEMList.Attribute("SPD").Value);
            }
    
            foreach (string slist in sellist.Distinct())
            {
                comboBox1.Items.Add(slist);
            }
    
    0 讨论(0)
  • 2020-11-22 03:38
    var distinctItems = items.GroupBy(x => x.Id).Select(y => y.First());
    
    0 讨论(0)
  • 2020-11-22 03:39

    An universal extension method:

    public static class EnumerableExtensions
    {
        public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> keySelector)
        {
            return enumerable.GroupBy(keySelector).Select(grp => grp.First());
        }
    }
    

    Example of usage:

    var lstDst = lst.DistinctBy(item => item.Key);
    
    0 讨论(0)
提交回复
热议问题