谁有一个快速的方法去重复在c#的泛型列表?


当前回答

如果需要比较复杂的对象,则需要在Distinct()方法中传递一个compararer对象。

private void GetDistinctItemList(List<MyListItem> _listWithDuplicates)
{
    //It might be a good idea to create MyListItemComparer 
    //elsewhere and cache it for performance.
    List<MyListItem> _listWithoutDuplicates = _listWithDuplicates.Distinct(new MyListItemComparer()).ToList();
        
    //Choose the line below instead, if you have a situation where there is a chance to change the list while Distinct() is running.
    //ToArray() is used to solve "Collection was modified; enumeration operation may not execute" error.
    //List<MyListItem> _listWithoutDuplicates = _listWithDuplicates.ToArray().Distinct(new MyListItemComparer()).ToList();

    return _listWithoutDuplicates;
}

假设你有另外两个类,比如:

public class MyListItemComparer : IEqualityComparer<MyListItem>
{
    public bool Equals(MyListItem x, MyListItem y)
    {
        return x != null 
               && y != null 
               && x.A == y.A 
               && x.B.Equals(y.B); 
               && x.C.ToString().Equals(y.C.ToString());
    }

    public int GetHashCode(MyListItem codeh)
    {
        return codeh.GetHashCode();
    }
}

And:

public class MyListItem
{
    public int A { get; }
    public string B { get; }
    public MyEnum C { get; }

    public MyListItem(int a, string b, MyEnum c)
    {
        A = a;
        B = b;
        C = c;
    }
}

其他回答

作为一个辅助方法(没有Linq):

public static List<T> Distinct<T>(this List<T> list)
{
    return (new HashSet<T>(list)).ToList();
}

扩展方法可能是一种不错的方式……就像这样:

public static List<T> Deduplicate<T>(this List<T> listToDeduplicate)
{
    return listToDeduplicate.Distinct().ToList();
}

然后像这样调用,例如:

List<int> myFilteredList = unfilteredList.Deduplicate();

我喜欢使用这个命令:

List<Store> myStoreList = Service.GetStoreListbyProvince(provinceId)
                                                 .GroupBy(s => s.City)
                                                 .Select(grp => grp.FirstOrDefault())
                                                 .OrderBy(s => s.City)
                                                 .ToList();

我的列表中有这些字段:Id、StoreName、City、PostalCode 我想在一个有重复值的下拉列表中显示城市。 解决方案:按城市分组,然后选择列表中的第一个城市。

使用Linq的Union方法。

注意:这个解决方案不需要了解Linq,只需要知道它存在。

Code

首先将以下内容添加到类文件的顶部:

using System.Linq;

现在,你可以使用下面的方法从一个名为obj1的对象中删除重复项:

obj1 = obj1.Union(obj1).ToList();

注意:将obj1重命名为对象的名称。

它是如何工作的

Union命令列出两个源对象的每个条目中的一个。由于obj1都是源对象,这将把obj1减少为每个条目中的一个。 ToList()返回一个新的List。这是必要的,因为像Union这样的Linq命令将结果返回为IEnumerable结果,而不是修改原来的List或返回一个新的List。

如果需要比较复杂的对象,则需要在Distinct()方法中传递一个compararer对象。

private void GetDistinctItemList(List<MyListItem> _listWithDuplicates)
{
    //It might be a good idea to create MyListItemComparer 
    //elsewhere and cache it for performance.
    List<MyListItem> _listWithoutDuplicates = _listWithDuplicates.Distinct(new MyListItemComparer()).ToList();
        
    //Choose the line below instead, if you have a situation where there is a chance to change the list while Distinct() is running.
    //ToArray() is used to solve "Collection was modified; enumeration operation may not execute" error.
    //List<MyListItem> _listWithoutDuplicates = _listWithDuplicates.ToArray().Distinct(new MyListItemComparer()).ToList();

    return _listWithoutDuplicates;
}

假设你有另外两个类,比如:

public class MyListItemComparer : IEqualityComparer<MyListItem>
{
    public bool Equals(MyListItem x, MyListItem y)
    {
        return x != null 
               && y != null 
               && x.A == y.A 
               && x.B.Equals(y.B); 
               && x.C.ToString().Equals(y.C.ToString());
    }

    public int GetHashCode(MyListItem codeh)
    {
        return codeh.GetHashCode();
    }
}

And:

public class MyListItem
{
    public int A { get; }
    public string B { get; }
    public MyEnum C { get; }

    public MyListItem(int a, string b, MyEnum c)
    {
        A = a;
        B = b;
        C = c;
    }
}