Entity Framework Core如何使用类似SQL的Select Case语句?

3

我有一个像这样的 SQL 查询:

Select 
    sum(case when OperationType = 1 then 1*ProductCountMain when OperationType = 2 then -1*ProductCountMain end) As Result
From Inventories

我该如何在EntityFramework Core中使用Lambda表达式编写这个代码?


1
你尝试过使用三元运算符 ?: 吗?例如 Inventories.Sum(i=>i.OperationType==1?ProductCountMain:-ProductCountMain) - Panagiotis Kanavos
1个回答

0

你可以使用三目运算符?:)来完成此操作:

var total=myContext.Inventories.Sum(i=>i.OperationType==1
                                      ?ProductCountMain
                                      :-ProductCountMain);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接