gpt4 book ai didi

sql - NHibernate/EF Linq 中的 Case 语句

转载 作者:行者123 更新时间:2023-12-04 06:14:42 25 4
gpt4 key购买 nike

什么将相当于 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

最佳答案

这应该可以帮助你

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(),
};

关于sql - NHibernate/EF Linq 中的 Case 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7403771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com