小编典典

使用LINQ(与GRO​​UP BY)编写案例陈述

sql

我真的在LINQ上挣扎。我已经用SQL编写了查询(实际上这是一个关键点),并试图在LINQ中编写等效的语句。任何前进的指针将不胜感激。参见下面的代码:

SELECT b.URL,
SUM(CASE WHEN (a.ExtFlag = 0) THEN 1 ELSE 0 END) AS IntLinks,   
SUM(CASE WHEN (a.ResponseCode >= 400 AND a.ExtFlag = 0) THEN 1 ELSE 0 END) AS IntBroken,
SUM(CASE WHEN (a.ExtFlag = 1) THEN 1 ELSE 0 END) AS ExtLinks,   
SUM(CASE WHEN (a.ResponseCode >= 400 AND a.ExtFlag = 1) THEN 1 ELSE 0 END) AS ExtBroken
FROM Link a
INNER JOIN Host b
ON a.HostID = b.ID
GROUP BY b.URL
ORDER BY b.URL

非常感谢。


阅读 172

收藏
2021-04-28

共1个答案

小编典典

我认为这应该可以满足您的要求-至少值得一试:

var query = from link in db.Links
            join host in db.Hosts on link.HostID equals host.ID
            group link by host.Url into links
            select new
            {
                Url = links.Url,
                IntLinks  = links.Count(link => link.ExtFlag == 0),
                IntBroken = links.Count(link => link.ExtFlag == 0 &&
                                                link.ResponseCode >= 400),
                ExtLinks =  links.Count(link => link.ExtFlag == 1),
                ExtBroken = links.Count(link => link.ExtFlag == 1 &&
                                                link.ResponseCode >= 400),
            };
2021-04-28