1

我有一个人类:

Public Class Person
{
public string Name {get; set;}
public string Id {get; set;}
}

Public EqualityOnPerson : IEqualityComparer<Person>
{
public bool Equals(PersonData x, PersonData y)
    {
        return x.Id == y.Id;
    }

    public int GetHashCode(Person obj)
    {
        return obj.Id.GetHashCode();
    }

}

我有一个包含重复 ID 的人员对象列表:

Person.Name = "ABC"
Person.Id = "123"

Person.Name = "CBA"
Person.Id = "123"

Person.Name = "DEF"
Person.Id = "456"

Person.Name = "GHI"
Person.Id = "789"

Person.Name = "JKL"
Person.Id = "789"

Person.Name = "MNO"
Person.Id = "789"

Person.Name = "PQR"
Person.Id = "101"

Person.Name = "STU"
Person.Id = "102"

使用 distinct 将给出

Person.Name = "CBA"
Person.Id = "123"

Person.Name = "DEF"
Person.Id = "456"

Person.Name = "GHI"
Person.Id = "789"

Person.Name = "PQR"
Person.Id = "101"

Person.Name = "STU"
Person.Id = "102"

我怎么得到这个

Person.Name = "DEF"
Person.Id = "456"

Person.Name = "PQR"
Person.Id = "101"

Person.Name = "STU"
Person.Id = "102"

只有那些不重复的人对象。

问候。

4

2 回答 2

2

您需要使用平等比较器将人员分组,然后从只有一个人的组中选择人员:

var unduplicatedPersons = persons
    .GroupBy(p => p, new EqualityOnPerson())
    .Where(g => g.Count() == 1)
    .Select(g => g.Key);

这是一个专用的扩展方法,如果需要,它可能会有轻微的性能优势(只是为了好玩):

public IEnumerable<T> DistinctOnly<T>(this IEnumerable<T> source,
                                      IEqualityComparer<T> comparer)
{
    HashSet<T> all = new HashSet<T>(comparer);
    HashSet<T> distinct = new HashSet<T>(comparer);
    foreach (T t in source)
    {
        if (all.Add(t))
            distinct.Add(t);
        else
            distinct.Remove(t);
    }
    return distinct;
}
于 2013-02-28T10:13:37.000 回答
0
var result = persons.Where(outer => 
     persons.Count(inner => inner.Id == outer.Id ) < 2)

这样你就不需要相等比较器了。

于 2013-02-28T10:21:42.887 回答