简体   繁体   English

从通用列表底部删除重复项

[英]Removing Duplicates from bottom of Generic List

I am trying to remove duplicates item from bottom of generic list. 我试图从通用列表的底部删除重复项。 I have class defined as below 我的课程定义如下

public class Identifier
{
    public string Name { get; set; }
}

And I have defined another class which implements IEqualityComparer to remove the duplicates from List 我已经定义了另一个实现IEqualityComparer的类来从List中删除重复项

public class DistinctIdentifierComparer : IEqualityComparer<Identifier>
{
    public bool Equals(Identifier x, Identifier y)
    {
        return x.Name == y.Name;
    }

    public int GetHashCode(Identifier obj)
    {
        return obj.Name.GetHashCode();
    }
}

However, I am trying to remove the old items and keep the latest. 但是,我正在尝试删除旧项目并保持最新状态。 For example if I have list of identifier defined as below 例如,如果我有如下定义的标识符列表

Identifier idn1 = new Identifier { Name = "X" };
Identifier idn2 = new Identifier { Name = "Y" };
Identifier idn3 = new Identifier { Name = "Z" };
Identifier idn4 = new Identifier { Name = "X" };
Identifier idn5 = new Identifier { Name = "P" };
Identifier idn6 = new Identifier { Name = "X" };

List<Identifier> list =  new List<Identifier>();
list.Add(idn1);
list.Add(idn2);
list.Add(idn3);
list.Add(idn4);
list.Add(idn5);
list.Add(idn6);

And I have implemented 我已经实施了

var res = list.Distinct(new DistinctIdentifierComparer());

How do I make sure by using distinct that I am keeping idn6 and removing idn1 and idn4? 我如何确保使用distinct我保持idn6并删除idn1和idn4?

Most LINQ operators are order-preserving : the API of Distinct() says it will take the first instance of each item it comes across. 大多数LINQ运算符都是保持顺序的 :Distinct()的API表示它会占用它遇到的每个项目的第一个实例。 If you want the last instance, just do: 如果你想要最后一个实例,只需:

var res = list.Reverse().Distinct(new DistinctIdentifierComparer());

Another option that would avoid you having to define an explicit comparer would be: 另一个避免必须定义显式比较器的选项是:

var res = list.GroupBy(i => i.Name).Select(g => g.Last());

From MSDN: 来自MSDN:

The IGrouping objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping. 基于源中产生每个IGroup的第一个键的元素的顺序,顺序产生IGrouping对象。 Elements in a grouping are yielded in the order they appear in source. 分组中的元素按它们在源中出现的顺序生成。

You could also implement a custom add method to maintain the latest records: 您还可以实现自定义添加方法来维护最新记录:

public class IdentifierList : List<Identifier>
{
    public void Add(Identifier item)
    {
        this.RemoveAll(x => x.Name == item.Name);
        base.Add(item);
    }
}

Identifier idn1 = new Identifier { Name = "X" };
Identifier idn2 = new Identifier { Name = "Y" };
Identifier idn3 = new Identifier { Name = "Z" };
Identifier idn4 = new Identifier { Name = "X" };
Identifier idn5 = new Identifier { Name = "P" };
Identifier idn6 = new Identifier { Name = "X" };

IdentifierList list = new IdentifierList ();
list.Add(idn1);
list.Add(idn2);
list.Add(idn3);
list.Add(idn4);
list.Add(idn5);
list.Add(idn6);

You could Group and check if any Count is > 1 您可以Group并检查是否有任何Count > 1

var distinctWorked = !(res
.GroupBy(a => a.Name)
.Select(g => new{g.Key, Count = g.Count()})
.Any(a => a.Count > 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM