2列"类别"和"子类别"的表数据
我想得到一个"类别"的集合,[子类别]使用下面的代码我得到重复.外部"from"之后的Puting .Distinct()没有多大帮助.我错过了什么?
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看起来像这样
category subcategory ---------------------- cat1 subcat1 cat1 subcat2 cat2 subcat3 cat2 subcat3
我得到的结果是
cat1, [subcat1,subcat2] cat1, [subcat1,subcat2]
但我只想要一个条目
用这段代码解决了我的问题:
var rootcategories2 = (from p in sr.products group p.subcategory by p.category into subcats select subcats);
现在也许是时候考虑什么是正确的问题了..( - :
用这个代码解决了
var rootcategories2 = (from p in sr.products group p.subcategory by p.category into subcats select subcats);
感谢大家