我有以下数据
分类
产品
物品
查询是什么,以便我得到多个列表,即
ListOfCategory包含CategoryName和ListOfProduct<>
ListOfProduct包含ProductName和ListOfItems<>
LISTOFITEMS<>包含ItemName和ItemID
var cats = (from g in CMP.tblCategories
join proc in CMP.tblProducts
on g.CategoryID equals proc.CategoryID
join item in CMP.tblItems
on proc.ProductID equals item.ProductID
select new { Cat = g.Name, Pro = proc.Name, Itm = item.Name, ItmID = item.ItemID });
我知道这是错的,所以请帮助我
答案 0 :(得分:2)
您可以在Linq中使用子查询来简化您的要求。
//获取包含所有详细信息的项目列表
var Items = (from g in CMP.tblCategories
join proc in CMP.tblProducts
on g.CategoryID equals proc.CategoryID
join item in CMP.tblItems
on proc.ProductID equals item.ProductID
select new { Category = g.Name, ProductName = proc.Name, ItemName = item.Name, ItemID = item.ItemID });
//Group by ProductName to get a list of ProductName , List<Items>
var Products = (from i in Items
group i by i.CategoryName into g
select new { CategoryName = g.Key,
Products = (from p in Items
group p by p.ProductName into Productgroup
select new {ProductName = Productgroup.Key, Items = Productgroup})
}).ToList();