gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:05:24 26 4
gpt4 key购买 nike

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

var list = ((List<SomeType>)xDic)
.GroupBy(x => new {x.Application, x.ExternalID})
.OrderByDescending(z => z.First().CreateDate)
.Select(y => y.First()).ToList();

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

总而言之,我需要根据 ((x.application/x.externalid) 或 (x.application/x.externaldisplayid)) 组合过滤掉任何重复项,从而获得一个唯一的 SomeTypes 列表。

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}

最佳答案

首先,声明两个相等比较器来指定你的两个条件,如下所示:

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 的文档不保证这一点,您可以使用如下自定义的 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());

关于c# - 使用 LINQ 从类型集合中过滤重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33641530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com