[c#]Linq 查询使用的组和有

标签: Linq SQLServer C#
发布时间: 2017/2/26 15:22:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以,我一直想复制这 SQL 查询︰

select COUNT(*) as Total_2_Review
from (
    select processed_image_id, count(*) as Total_Confirm from dbo.history 
    where action_id=104
    group by processed_image_id
    having count(*) > 1
) as tbl

Linq ,如下所示︰

var total2Review = (from h in secondDb.Histories.Where(i => i.ActionId == 104)
                    group h by new { h.ActionId, h.ProcessedImageId } into g
                    where g.Key.ActionId > 1
                    select g).Count();

然而,我知道它不应该是正确的因为我不在我 group 子句选择实际计数大于 1。

如何可以实现为 LINQ 查询的这个 SQL 查询?

解决方法 1:

改变 Key.ActionId.Count()wheregroup by :

var total2Review = (from h in secondDb.Histories.Where(i => i.ActionId == 104)
                    group h by new { h.ActionId, h.ProcessedImageId } into g
                    where g.Count()> 1
                    select g).Count();
赞助商