使用LINQ从类型集合中过滤重复项

时间:2015-11-10 23:18:27

标签: c# .net linq duplicates filtering

我通过对两个参数进行分组并根据createdate选择子组列表中的最新类型(使用first())来过滤列表。 这消除了x.application和x.externalid属性的重复。

Example set:
{ "extID": 1234, "extDspID" : 111, "App" : "Test", "CreateDate": 2/01/2015}
{ "extID": 1234, "extDspID" : 5, "App" : "Test", "CreateDate": 1/01/2015}
{ "extID": 012, "extDspID" : 90, "App" : "Mono", "CreateDate": 6/06/2015}
{ "extID": 999, "extDspID" : 78, "App" : "Epic", "CreateDate": 8/08/2015}
{ "extID": 333, "extDspID" : 78, "App" : "Epic", "CreateDate": 8/12/2015}
{ "extID": 345, "extDspID" : 33, "App" : "Test", "CreateDate": 2/01/2015}
{ "extID": 666, "extDspID" : 33, "App" : "Test", "CreateDate": 1/01/2015}

desired result:
{ "extID": 1234, "extDspID" : 111, "App" : "Test", "CreateDate": 2/01/2015}
{ "extID": 012, "extDspID" : 90, "App" : "Mono", "CreateDate": 6/06/2015}
{ "extID": 333, "extDspID" : 78, "App" : "Epic", "CreateDate": 8/12/2015}
{ "extID": 345, "extDspID" : 33, "App" : "Test", "CreateDate": 2/01/2015}

我遇到的问题是定义另一个属性组合(x.application和x.externaldisplayid)来过滤和分组来获取第一个属性。

总而言之,我需要通过根据((x.application / x.externalid)OR(x.application / x.externaldisplayid))组合过滤掉任何重复项来获取一个独特的SomeTypes列表。

=INDEX(A2:A6,MATCH(0,COUNTIF(A2:A6,">"&A2:A6),))

2 个答案:

答案 0 :(得分:3)

首先,声明两个相等比较来指定你的两个条件:

public class MyEqualityComparer1 : IEqualityComparer<SomeType>
{
    public bool Equals(SomeType x, SomeType y)
    {
        return x.Application == y.Application && x.ExternalID == y.ExternalID;
    }

    public int GetHashCode(SomeType obj)
    {
        return (obj.Application + obj.ExternalID).GetHashCode();
    }
}

public class MyEqualityComparer2 : IEqualityComparer<SomeType>
{
    public bool Equals(SomeType x, SomeType y)
    {
        return x.Application == y.Application && x.ExternalDisplayId == y.ExternalDisplayId;
    }

    public int GetHashCode(SomeType obj)
    {
        return (obj.Application + obj.ExternalDisplayId).GetHashCode();
    }
}

然后,按CreatedDate订购您的列表,然后使用Distinct过滤您的列表,如下所示:

var result = xDic
    .OrderByDescending(x => x.CreateDate)
    .Distinct(new MyEqualityComparer1())
    .Distinct(new MyEqualityComparer2());

Distinct方法should remove the later items,因此我们应该能够依赖于我们使用OrderByDescending确保Distinct删除项目较少的事实CreatedTime

但是,由于Distinct的文档不能保证这一点,您可以使用这样的自定义不同方法:

public static class Extensions
{
    public static IEnumerable<T> OrderedDistinct<T>(this IEnumerable<T> enumerable, IEqualityComparer<T> comparer)
    {
        HashSet<T> hash_set = new HashSet<T>(comparer);

        foreach(var item in enumerable)
            if (hash_set.Add(item))
                yield return item;
    }
}

并像这样使用它:

var result = xDic
    .OrderByDescending(x => x.CreateDate)
    .OrderedDistinct(new MyEqualityComparer1())
    .OrderedDistinct(new MyEqualityComparer2());

答案 1 :(得分:0)

当前接受的答案不会对您的&#34; SomeType&#34;对象正确,因此不会产生您想要的结果集。

我在这里实施了一个解决方案:

https://dotnetfiddle.net/qBkIXo

我的解决方案也基于Distinct(请参阅MSDN文档here)。我生成散列的方式基于this整齐的方法,它使用匿名类型,例如。

public int GetHashCode(SomeType sometype)
{
 //Calculate the hash code for the SomeType.
 return new { sometype.Application, sometype.ExternalID }.GetHashCode();
}

为了达到正确的预期效果,需要应用不同组合,排序和使用的组合,例如

    var noduplicates = products.GroupBy(x => new {x.Application, x.ExternalDisplayId})
        .Select(y => y.OrderByDescending(x => x.CreateDate).First())
        .ToList()
        .Distinct(new ApplicationExternalDisplayIdComparer())
        .GroupBy(x => new {x.Application, x.ExternalID})
        .Select(y => y.OrderByDescending(x => x.CreateDate).First())
        .ToList()
        .Distinct(new ApplicationExternalIDComparer());

正如您在小提琴输出中看到的那样,这会给出您期望的结果。