我有一个包含具有groupnumber属性的对象的IEnumerable。我希望能够获得具有重复分组编号的所有对象的列表,例如
obj1:groupnumber = 1 KEEP
obj2:groupnumber = 2 DELETE
obj3:groupnumber = 1 KEEP
我可以使用以下内容获取所有重复的组号的列表
var duplicates = from c in sorted
group c by c.groupnumber into g
where g.Count() > 1
select new { groupnumber = g.Key, recs = g.Count() };
但我无法弄清楚如何清除所有单个实例对象的列表
干杯
答案 0 :(得分:1)
这是最简单的选项(我认为):
sorted.GroupBy( c => c.groupnumber )
.Where( g => g.Count() > 1 )
.SelectMany( g => g );
或者,尝试以下方法:
var duplicates = from c in sorted
group c by c.groupnumber into g
where g.Count() > 1
select g.Key;
// convert the list to a lookup object for efficiency
var dupLookup = duplicates.ToLookup( c => c );
// exclude all items that are NOT one of the duplicate group keys
var excludeNonDups = sorted.Where( c => !dupLookup.Contains( c ) )
答案 1 :(得分:1)
好吧,我必须读几遍你的问题。我的理解是你想“选择集合中有多个obj的所有obj,并且具有相同的groupnumber”...所以过滤掉具有唯一groupnumbers的obj。
如果是这样的话,那你几乎就到了!使用SelectMany
将群组折叠为单个群集。
var duplicates = (from c in sorted
group c by c.groupnumber into g
where g.Count() > 1
select g).SelectMany(grp => grp);
答案 2 :(得分:0)
如果您只想要其中一个重复项,请添加对Distinct()的调用:
var duplicates = (from c in sorted group c by c.groupnumber into g where g.Count() > 1 select new { groupnumber = g.Key, recs = g.Count() }).Distinct();