【问题标题】:Need help on Linq with group by and distinct在 Linq 上需要 group by 和 distinct 的帮助
【发布时间】:2012-07-23 22:49:19
【问题描述】:

我正在尝试执行一个组,后跟一个 distinct 来验证一组列是否仅映射到其他列。例如,在下面的数据集中

Brand   Product      Location      Customer      Demand
 Box       A         Chicago       Chicago        10
 Box       B         Chicago       Milwaukee      20
 Cart      C         Madison       Milwaukee      10
 Cart      D         Chicago       Milwaukee      15

产品 A、B、C 有效。但 D 无效,因为存在以芝加哥为位置、密尔沃基为客户的产品 B。 我正在尝试构建一个 LINQ 查询来获取异常记录并遇到一些麻烦。我很确定我的查询过于复杂了。

var vDuplicateSupplierLitho = from p in vRecords
                              group p by new
                              {
                                  Location = p["Location"].Cast<string>(),
                                  Customer = p["Customer"].Cast<string>()
                              } into grp
                              select new
                              {
                                  Location = grp.Key.Location,
                                  Customer = grp.Key.Customer,
                                  Products = from a in grp
                                             group a by new { Product = a["Product"].Cast<string>() } into apngrp
                                             where apngrp.Count() > 1 && apngrp.Select(a => a["Product"]).Distinct().Count() > 1
                                                  from NonUniqueRecord in apngrp
                                                  select new
                                                  {
                                                      Product = apngrp.key.Product,
                                                      Location = g.key.Location,
                                                      Customer = g.key.Customer
                                                      Demand = NonUniqueRecord["Demand"]
                                                  }
                                              };

谢谢。

【问题讨论】:

  • 这是 Linq-To-DataSet 吗? “但 D 无效,因为存在以芝加哥为位置,密尔沃基为客户的产品 A” 是什么意思? Product AChicago 客户。
  • 嗨,是的,这是 Linq to dataset。对不起,有一个类型。它应该是产品 B 和 D,它们都有相同的位置和客户,它不应该是有效的。所以我需要返回两个记录。

标签: c# linq group-by


【解决方案1】:
new[] {
        new { Brand = "Box", Product = "A", Location = "Chicago", Customer = "Chicago", Demand = 10 },
        new { Brand = "Box", Product = "B", Location = "Chicago", Customer = "Milwaukee", Demand = 20 },
        new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 },
        new { Brand = "Cart", Product = "D", Location = "Chicago", Customer = "Milwaukee", Demand = 15 },
        new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 }
        }
    .GroupBy(o => new { o.Location, o.Customer })
    .Where(g => g.Select(o => o.Product).Distinct().Count() > 1)
    .SelectMany(g => g)

...或者,在查询语法中...

from record in new[] {
        new { Brand = "Box", Product = "A", Location = "Chicago", Customer = "Chicago", Demand = 10 },
        new { Brand = "Box", Product = "B", Location = "Chicago", Customer = "Milwaukee", Demand = 20 },
        new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 },
        new { Brand = "Cart", Product = "D", Location = "Chicago", Customer = "Milwaukee", Demand = 15 },
        new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 }
        }
group record by new { record.Location, record.Customer } into grouping
where (from o in grouping select o.Product).Distinct().Count() > 1
from duplicate in grouping
select duplicate

【讨论】:

  • 感谢您的样品。此查询仅按位置、客户获取分组记录,但不考虑产品的区别值。在上面的示例中,产品 B 和 D 具有相同的位置和客户,因此它们显示为无效。但是,如果我将产品 D 也重命名为 B,那么查询也会将它们显示为异常。我正在寻找这样一种情况,一旦我们按位置分组,客户我需要检查独特产品列表并确保集合 = 1。希望我没有混淆你。
  • 我想我跟着。现在好点了吗?
  • 谢谢。我现在试一试。有没有办法在没有 lambda 表达式的情况下进行此查询?只是问问。如果这很困难,没关系
猜你喜欢
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多