【发布时间】:2014-12-09 15:38:32
【问题描述】:
我正在尝试将有效的 SQL 查询转换为等效的 LINQ。 这是查询。
SELECT REPORT_NUMBER,
case when count(distinct STATE) > 1 then 'PENDING'
else case when max(STATE) = 'REPORTED' then 'REPORTED'
else 'PENDING' end end status,
max(REPORT_YEAR)
FROM SAMPLE
GROUP BY REPORT_NUMBER
ORDER BY max(REPORT_YEAR) DESC
到目前为止,我创建了需要帮助的 LINQ 查询。
var sums = from foo in db.SAMPLEs
group foo by foo.REPORT_NUMBER into groupings
orderby groupings.Key ascending
select new ReportListModel
{
ReportNbr = groupings.Key,
ReportYear = groupings.Max(g => g.REPORT_YEAR),
ReportSt = groupings.Max(g => g.STATE)
};
使用groupings.Max(g => g.STATE) 给了我正确的记录数量,但显然给了我不正确的字段结果。
如何像在 SQL 查询中一样创建 case 语句?
【问题讨论】: