I'm new to Linq and I'm trying to write a query using the northwind database which should return all suppliers who have two or more products in the same category.
var test1 =
(from p in Products
join pi in Products on p.CategoryID equals pi.CategoryID
join pf in Products on p.SupplierID equals pf.SupplierID
where p.ProductID != pi.ProductID
select new
{p.ProductName, p.CategoryID, p.SupplierID}).ToList().Distinct();
test1.Dump();
This was my last try that didn't work. I'm a bit resigned because I've been trying to figure this out for hours and it still won't do what it's supposed to. Maybe I'm just getting it completely wrong?
My approach was that there has to be two or more listings with the same SupplierID and CategoryID but different ProductID, but yet I haven't found a solution.
This is better done using a GroupBy() :