简体   繁体   English

如何进行多对多LINQ实体分组查询?

[英]How to do Many To Many LINQ entity grouping queries?

I can see many examples on this site of the sort of queries I'm after, but I can't relate to them and how they work. 我可以在该站点上看到许多我所关注的查询示例,但我与它们以及它们的工作方式无关。 I was wondering if you could help me. 我想知道你是否能帮助我。

I have set up my many-to-many tables and relationships with entity designer in Visual Studio. 我已经在Visual Studio中建立了多对多表,并与实体设计器建立了关系。

tblQuotes
ID | QuoteNo | Date

tblItems
ID | PartNo | Desc

tblSuppliers
ID | Supplier | email

tblQIS (quotes items suppliers)
ID | SupplierID | QuoteID | ItemID

I've put some test data in and have begun to try to type this, but I think I first need to group by quoteNo then group by supplier, to get the details in the correct view. 我已经输入了一些测试数据并开始尝试键入此数据,但我认为我首先需要按报价分组,然后按供应商分组,以在正确的视图中获取详细信息。

var tblQuotes = from d in db.tblQuotes_Items_Suppliers
                    .Include(t => t.tblItems)
                    .Include(t => t.tblQuotes)
                    .Include(t => t.tblSuppliers)
                group by (d.QuoteID,d.SupplierID)
                select d;

Can anyone help me out? 谁能帮我吗?

you could try this: 您可以尝试以下方法:

var tblQuotes = 
    from d in db.tblQuotes_Items_Suppliers    // or tblQIS whatever the name
    group d by new
    {
        d.QuoteID,
        d.SupplierID
    }  into g
    select g;

that would give you Quotes grouped by both QuoteID and SupplierID 这将为您提供按QuoteID和SupplierID分组的报价

Update 更新

the tblQuotes is a list (IQueryable) of grouped quotes, so you can access other entities as follow: tblQuotes是分组报价的列表(IQueryable),因此您可以按以下方式访问其他实体:

 var firstGroupOfQuotes = tblQuotes.First(); // will give you the first group of quotes
 var firstQuote = firstGroupOfQuotes.First(); // will give you the first quote in the first group
 var item = firstQuote.tblItems; // will give you the item of this quote
 var partNo = item.PartNo; // will give you the PartNo of this item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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