繁体   English   中英

在Linq上需要帮助,分组依据不同

[英]Need help on Linq with group by and 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,其中芝加哥为Location,密尔沃基为客户。 我试图建立一个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"]
                                                  }
                                              };

谢谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM