简体   繁体   English

LINQ加入/分组问题

[英]LINQ Join/Grouping issues

So I am stuck again... 所以我又被卡住了...

I have three tables that I am trying to join and group with linq; 我有三个表要与linq一起加入并分组。 Company, Notices and Requests. 公司,通知和要求。

  • A company could have many notices. 一个公司可能有很多通知。

  • A notice have a SubcategoryId and a CompanyId 通知中包含SubcategoryId和CompanyId

  • A request have a SubcategoryId. 请求具有SubcategoryId。

My aim here is to get a list of companies and for each company get a list of their noticed subcategories and for each noticed subcategory the requests within that subcategory 我的目的是获取公司列表,每个公司都获得其关注子类别的列表,并且对于每个关注子类别,该子类别中的请求

Ex: Company x is monitoring the subcategories 4 and 7. Show him what requests we've got within those subcats. 例如: x公司正在监视子类别4和7。向他展示我们在这些子类别中有什么要求。

This is how I do it for only one company. 这就是我仅对一家公司执行的操作。 But I can't solve the grouping and joins when I need a list of multiple companies. 但是当我需要多个公司的列表时,我无法解决分组和加入的问题。

Company company = db.Companies.Where(x=>x.UserId == 10).FirstOrDefault();
            var requestByNotices =
            from notice in company.Notices
            join request in db.Requests.Where(z => z.IsApproved &
                 z.Status.Status == "Active") on notice.SubcategoryId equals
                 request.Subcategoryid

UPDATE UPDATE

Let me rephrase my questions. 让我改一下我的问题。

This query is producing a table with the results I am looking for: 此查询将生成一个表,其中包含我要查找的结果:

SELECT Companies.CompanyId, Companies.Name, Account.Email, Notices.SubcategoryId, Requests.FriendlyName FROM Companies
INNER JOIN Notices  ON Companies.CompanyId = Notices.CompanyId 
INNER JOIN Account  ON Account.UserId = Companies.Uid
INNER JOIN Requests ON Notices.SubcategoryId = Requests.Subcategoryid 
WHERE Requests.StartDate > '2013-12-22';  

But instead of generating a flat list I would like to translate this into to linq but also group by CompanyId. 但是除了生成平面列表,我想将其转换为linq,还可以按CompanyId分组。 So each company is represented by one row only and within that company I can get the requests. 因此,每个公司仅由一行代表,并且在该公司内我可以收到请求。

So instead of a list like this: 因此,而不是像这样的列表:

CompanyId   Name   Email            SubcategoryId     FriendlyName
1           Test   test@test.com    2                 x
1           Test   test@test.com    4                 y

I would like to have it like this: 我想要这样的:

CompanyId   Name   Email            GroupOfRequests  
1           Test   test@test.com    contains two 

Thankful for any help! 感谢您的帮助!

UPDATED: if i understand right you want something like this 更新:如果我理解正确,你想要这样的东西

var result =
        from company in db.Companies
        from notice in company.Notices
        join request in db.Requests.Where(z => z.IsApproved &&
             z.Status.Status == "Active") on notice.SubcategoryId equals request.Subcategoryid
        group new {notice, request } by company into gr           
        select new {gr.Key, Value = gr.ToList() }

UPDATE2 UPDATE2
for your sql it seems like this 为您的SQL似乎是这样的

from company in db.Companies
join notice in db.Notices on company.CompanyId equals notice.CompanyId
join account in db.Account on company.Uid equals account.UserId
join request in db.Requests on notice.SubcategoryId equals request.SubcategoryId

group new {notice, request} by new {company, account} into g
select new {g.Key, value = g}

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

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