0

一連の列が他の列のみにマップされているかどうかを確認するために、グループの後に個別を実行しようとしています。たとえば、以下のデータセットでは

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"]
                                                  }
                                              };

ありがとう。

4

1 に答える 1

5
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
于 2012-07-23T22:55:20.513 に答える