Remove objects with a duplicate property from List

后端 未结 4 555
陌清茗
陌清茗 2021-02-04 23:03

I have a List of objects in C#. All of the objects contain a property ID. There are several objects that have the same ID property.

How can I trim the List (or make a

相关标签:
4条回答
  • 2021-02-04 23:32

    MoreLINQ DistinctBy() will do the job, it allows using object proeprty for the distinctness. Unfortunatly built in LINQ Distinct() not flexible enoght.

    var uniqueItems = allItems.DistinctBy(i => i.Id);
    

    DistinctBy()

    Returns all distinct elements of the given source, where "distinctness" is determined via a projection and the default eqaulity comparer for the projected type.

    • Download MoreLINQ
    • DistinctBy() sources

    PS: Credits to Jon Skeet for sharing this library with community

    0 讨论(0)
  • 2021-02-04 23:33

    Not sure if anyone is still looking for any additional ways to do this. But I've used this code to remove duplicates from a list of User objects based on matching ID numbers.

    private ArrayList RemoveSearchDuplicates(ArrayList SearchResults)
    {
        ArrayList TempList = new ArrayList();
    
        foreach (User u1 in SearchResults)
        {
            bool duplicatefound = false;
            foreach (User u2 in TempList)
                if (u1.ID == u2.ID)
                    duplicatefound = true;
    
            if (!duplicatefound)
                TempList.Add(u1);
        }
        return TempList;
    }
    

    Call: SearchResults = RemoveSearchDuplicates(SearchResults);

    0 讨论(0)
  • 2021-02-04 23:46

    If you want to avoid using a third-party library, you could do something like:

    var bar = fooArray.GroupBy(x => x.Id).Select(x => x.First()).ToList();
    

    That will group the array by the Id property, then select the first entry in the grouping.

    0 讨论(0)
  • 2021-02-04 23:56
    var list = GetListFromSomeWhere();
    var list2 = GetListFromSomeWhere();
    list.AddRange(list2);
    
    ....
    ...
    var distinctedList = list.DistinctBy(x => x.ID).ToList();
    

    More LINQ at GitHub

    Or if you don't want to use external dlls for some reason, You can use this Distinct overload:

    public static IEnumerable<TSource> Distinct<TSource>(
        this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
    

    Usage:

    public class FooComparer : IEqualityComparer<Foo>
    {
        // Products are equal if their names and product numbers are equal.
        public bool Equals(Foo x, Foo y)
        {
    
            //Check whether the compared objects reference the same data.
            if (Object.ReferenceEquals(x, y)) return true;
    
            //Check whether any of the compared objects is null.
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;
    
            return x.ID == y.ID
        }
    }
    
    
    
    list.Distinct(new FooComparer());
    
    0 讨论(0)
提交回复
热议问题