[英]Write a complex Linq2SQL query
I have Following Data 我有以下数据
Category 类别
Product 产品
Item 项目
What will be the query so that i will get multiple list ie 什么是查询,以便我将获得多个列表,即
ListOfCategory contains CategoryName and ListOfProduct<> ListOfCategory包含CategoryName和ListOfProduct <>
ListOfProduct contains ProductName and ListOfItems<> ListOfProduct包含ProductName和ListOfItems <>
ListOfItems<> Contains ItemName and ItemID 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 });
I know this is so wrong, So Kindly Help me 我知道这是错的,所以请帮帮我
you can use sub queries in Linq to simplyfy your requirement. 您可以在Linq中使用子查询来简单地满足您的要求。
//get the list of items with all details //获取所有详细信息的项目列表
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.