如何将带有case的sql sum转换为linq

时间:2015-10-02 15:16:36

标签: linq sum case

SUM(CASE WHEN [dbo].[tblHits].IsLike = 1 THEN 1 ELSE 0 END) as Likes     

我想要这样的事情,下面的代码给我错误

不能隐式转换类型bool?小数?

TotalLikes = grouped.Sum(d => d.IsLike == true ? d.IsLike = true : d.IsLike = false )

1 个答案:

答案 0 :(得分:1)

最直接的翻译是:

TotalLikes = grouped.Sum(d => d.IsLike == true ? 1 : 0)

可以缩短为:

TotalLikes = grouped.Sum(d => d.IsLike ? 1 : 0)

但我会用:

TotalLikes = grouped.Count(d=>d.IsLike);