简体   繁体   English

编写复杂的Linq2SQL查询

[英]Write a complex Linq2SQL query

I have Following Data 我有以下数据

Category 类别

  • CategoryName 分类名称
  • CategoryID 分类ID

Product 产品

  • ProductID 产品编号
  • ProductName 产品名称
  • CategoryID 分类ID

Item 项目

  • ItemID 物品ID
  • ItemName 项目名称
  • ProductID 产品编号

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM