首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写复杂的Linq2SQL查询

编写复杂的Linq2SQL查询
EN

Stack Overflow用户
提问于 2015-05-04 08:44:29
回答 1查看 86关注 0票数 0

我有以下数据

范畴

  • CategoryName
  • CategoryID

产品

  • ProductID
  • ProductName
  • CategoryID

项目

  • ItemID
  • ItemName
  • ProductID

什么是查询,以便我得到多个列表,

ListOfCategory包含CategoryName和ListOfProduct<>

ListOfProduct包含ProductName和ListOfItems<>

ListOfItems<>包含ItemName和ItemID

代码语言:javascript
复制
 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 });

我知道这很不对,所以请帮帮我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-04 09:16:47

您可以使用Linq中的子查询简化您的需求。

//获取包含所有详细信息的项目列表

代码语言:javascript
复制
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();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30025991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档