什么是“当发生时的情况”? (T-SQL)使用实体框架? [英] What is the equivalent of "CASE WHEN THEN" (T-SQL) with Entity Framework?
本文介绍了什么是“当发生时的情况”? (T-SQL)使用实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Transact-SQl请求,我经常使用它,并且我想获得与Entity Framework相同的请求。但是我不知道如何用EF进行 CASE WHEN声明。这是我的请求的简化代码:
I have a Transact-SQl request that I use a lot and I want to get the equivalent with Entity Framework. But I don't know how to make a "CASE WHEN" statement with EF. Here is a simplified code of my request :
SELECT Code,
SUM(CASE WHEN Month=1 THEN Days Else 0 END) AS Jan,
FROM MyTable
GROUP BY Code
告诉我是否可以使用EF,以及如何使用EF?
Can you tell me if it's possible and how to do this with EF ?
推荐答案
在这种情况下,我会说条件运算符( p?x:y
)是
In this case, I'd say the conditional operator (p ? x : y
) is a good substitute.
// context.MyTable is an IQueryable<MyTable>
var query = from t in context.MyTable
group t by t.Code into grp
select
new {
Code = grp.Key,
Jan = grp.Sum(x => x.Month == 1 ? x.Days : 0),
};
或将 Where
和总和
:
Jan = grp.Where(x => x.Month == 1).Sum(x => x.Days),
我不确定这些SQL确切地解释为什么,但是它们都应具有相同的结果。
I'm not sure what SQL these translate to exactly, but they should both have the same result.
这篇关于什么是“当发生时的情况”? (T-SQL)使用实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文