【发布时间】:2017-10-12 00:59:40
【问题描述】:
我有一个如下所示的列表:
Id: 1
Line1: Microsoft
Line2: Windows
Line3: Server
Line4: null
Line5: null
Id: 2
Line1: Microsoft
Line2: Windows
Line3: Server
Line4: null
Line5: null
Id: 3
Line1: Microsoft
Line2: Windows
Line3: Server
Line4: Development
Line5: null
现在我想合并/删除所有重复项(Id 1 和 2)。 这怎么可能?
我尝试过这样的事情:
result = result.DistinctBy(x => x.Line3).ToList();
但它也会删除不正确的 ID 3。
预期输出:
Id: 2 // can also be 1, doesn't matter
Line1: Microsoft
Line2: Windows
Line3: Server
Line4: null
Line5: null
Id: 3
Line1: Microsoft
Line2: Windows
Line3: Server
Line4: Development
Line5: null
【问题讨论】:
-
你是什么意思“汇集所有重复”?显示您的预期输出。
-
抱歉,我想删除它们。看看我的编辑@MrZander
-
如果它们是重复的,保留 1 或 2 是否重要?
-
是的,我需要保留其中一个,尽管@msitt 的 ID 并不重要
-
在列表中的对象上加上
IEqualityComparer,然后调用.Distinct()?