Linq中以下SQL语句的等价物是什么?我正在专门研究如何在Linq表达式中替换CASE语句。
SELECT a.app_id AS Id,
a.app_nm AS [Name],
COUNT(CASE
WHEN l.level_nm = 'ERROR' THEN l.log_id
ELSE NULL
END) AS Error,
COUNT(CASE
WHEN l.level_nm = 'FATAL' THEN l.log_id
ELSE NULL
END) AS Fatal
FROM info_log l
INNER JOIN application_info a
ON a.app_id = l.app_id
WHERE l.level_nm IN ( 'ERROR', 'FATAL' )
GROUP BY a.app_id,
a.app_nm
答案 0 :(得分:3)
这应该可以帮到你
var query = from l in context.info_log
from a in context.application_info
where l.app_id == a.app_id
where l.level_nm == "ERROR" || l.level_nm == "FATAL"
group l by new { a.app_id, a.app_nm } into lg
select new
{
Id = lg.Key.app_id,
Name = lg.Key.app_nm,
Error = lg.Where(x => x.level_nm == "ERROR").Count(),
Fatal = lg.Where(x => x.level_nm == "FATAL").Count(),
};