C# 从子组中选择重复项

Posted

技术标签:

【中文标题】C# 从子组中选择重复项【英文标题】:C# select duplicates from sub-group 【发布时间】:2021-10-28 05:03:54 【问题描述】:

我想过滤掉 "" 名称,然后选择每个存在重复 ID 的唯一位置,而不考虑名称:

数据设置

var list = new[]

    new  id = 3, Name = "", Location = "LocationA" ,
    new  id = 2, Name = "", Location = "LocationA" ,
    new  id = 1, Name = "T", Location = "LocationB" ,
    new  id = 2, Name = "H", Location = "LocationB" ,
    new  id = 3, Name = "E", Location = "LocationB" ,
    new  id = 3, Name = "R", Location = "LocationB" ,
    new  id = 5, Name = "U", Location = "LocationC" ,
    new  id = 5, Name = "S", Location = "LocationC" ,
    new  id = 5, Name = "S", Location = "LocationD" ,
    new  id = 4, Name = "O", Location = "LocationD" ,
    new  id = 4, Name = "Z", Location = "LocationE" ,
;

查询

var query1 = list
    .Where(s => s.Name != "")
    .GroupBy(g => g.Location)
    .Where(w => w.Select(s => s.Location).Count() > 1)
    .SelectMany(s => s)
    .GroupBy(g => g.id)
    .Where(w => w.Select(s => s.id).Count() > 1)
    .SelectMany(s => s)
    .ToList();
Console.WriteLine("output\n" + string.Join("\n", query1));

退货

 id = 3, Name = E, Location = LocationB 
 id = 3, Name = R, Location = LocationB 
 id = 5, Name = U, Location = LocationC 
 id = 5, Name = S, Location = LocationC 
 id = 5, Name = S, Location = LocationD 

vs我真正想要的

 id = 3, Name = E, Location = LocationB 
 id = 3, Name = R, Location = LocationB 
 id = 5, Name = U, Location = LocationC 
 id = 5, Name = S, Location = LocationC 

LocationD 的 ID 为 4 和 5,因此它应该被过滤掉,但我无法这样做。我究竟做错了什么?如何更正?

【问题讨论】:

我猜你又在问类似的问题,***.com/q/69733137/6299857 这能回答你的问题吗? C# select duplicate using group 不,不是,如果您使用该查询的输入运行该查询的解决方案,您将获得 7 个返回与 4 的实际答案。这里的查询不同,我希望先进行多个分组或分组然后再次重新组合,而不是之前对单个组的查询。 【参考方案1】:

给定

var list = new[]

   new  id = 3, Name = "", Location = "LocationA" ,
   new  id = 2, Name = "", Location = "LocationA" ,
   new  id = 1, Name = "T", Location = "LocationB" ,
   new  id = 2, Name = "H", Location = "LocationB" ,
   new  id = 3, Name = "E", Location = "LocationB" ,
   new  id = 3, Name = "R", Location = "LocationB" ,
   new  id = 5, Name = "U", Location = "LocationC" ,
   new  id = 5, Name = "S", Location = "LocationC" ,
   new  id = 5, Name = "S", Location = "LocationD" ,
   new  id = 4, Name = "O", Location = "LocationD" ,
   new  id = 4, Name = "Z", Location = "LocationE" ,
;

示例

var results = list
   .Where(s => s.Name != "")
   .GroupBy(x => new x.id, x.Location)
   .Where(g => g.Count() > 1)
   .SelectMany(y => y);

foreach (var result in results)
   Console.WriteLine($"result.id, result.Name, result.Location");

输出

3, E, LocationB
3, R, LocationB
5, U, LocationC
5, S, LocationC

【讨论】:

您好 TheGeneral,您昨天已经回答了同一个 OP 提出的相同问题。我不知道为什么 OP 会问类似的问题,而不是自己阅读并付出一些努力 @PrasadTelkikar 啊,是的。 ops 当前的尝试肯定是过头了 感谢 Gen 的帮助,我昨天在工作中尝试了以前的解决方案,然后意识到这不是我想要的,所以我再次完善了我的查询。您的解决方案在这里很完美,可以解决问题【参考方案2】:

idLocation 分组。并且得到.Count()超过1个。

var query1 = list
    .Where(s => s.Name != "")
    .GroupBy(g => new  g.Location, g.id )
    .Where(g => g.Count() > 1)
    .SelectMany(g => g)
    .ToList();

Sample demo

【讨论】:

以上是关于C# 从子组中选择重复项的主要内容,如果未能解决你的问题,请参考以下文章

C# 使用组选择重复项

C# linq 从 XElement 树中选择重复项

删除 SQL 选择中的重复项

MS ACCESS 2007 - 从子表单中的组合框中选择值会创建重复值

从子目录中选择文件并打印每个文件中的某一行[重复]

XSLT / Muenchian 分组:如何从组中选择具有某些子元素的元素?