I am trying to perform a group by followed by a distinct to verify if a set of columns maps to only other column. For example, in the dataset below
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
Product A, B, C are valid. But D is not valid since there exists a Product B with Chicago as Location & Milwaukee as Customer.
I am trying to build a LINQ query to get the exception record and having some trouble. I am very sure I have overcomplicated my query.
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"]
}
};
Thanks.
… or, in query syntax …