table data of 2 columns "category" and "subcategory"
i want to get a collection of "category", [subcategories] using code below i get duplicates. Puting .Distinct() after outer "from" does not help much. What do i miss?
var rootcategories = (from p in sr.products
orderby p.category
select new
{
category = p.category,
subcategories = (
from p2 in sr.products
where p2.category == p.category
select p2.subcategory).Distinct()
}).Distinct();
sr.products looks like this
category subcategory
----------------------
cat1 subcat1
cat1 subcat2
cat2 subcat3
cat2 subcat3
what i get in results is
cat1, [subcat1,subcat2]
cat1, [subcat1,subcat2]
but i only want one entry
solved my problem with this code:
var rootcategories2 = (from p in sr.products
group p.subcategory by p.category into subcats
select subcats);
now maybe it is time to think of what was the right question.. (-: