C#lambda表达式中的SQL Count(*)

时间:2018-05-16 09:51:37

标签: c# sql lambda count

如何在lambda表达式中实现Count(*)的SQL查询?

我想要的是转换这样的查询:

SELECT SendType, 
    CASE WHEN Date IS NULL THEN
        'NEW'
    ELSE
        'NOT NEW'
    End Registry,
    Count(*) as TotalSent
FROM [dbo].[Sends]
WHERE Disabled = 0
GROUP BY 
    SendType, 
    Registry

这样的lambda表达式:

context.EnviosPendientes
    .Where(ep => ep.Disabled == false)                        
    .Select(ep => new {
        SendType = ep.SendType,
        Registry = ep.Date == null ? "NEW" : "NOT NEW"
    })
    .GroupBy(ep => new { ep.TipoEnvio, ep.TipoReg })

但我不知道如何在结果中加入Count(*) as TotalSent

1 个答案:

答案 0 :(得分:0)

添加如下内容:

.GroupBy(ep => new { ep.SendType, ep.Registry })
.Select(ep => new
{
    ep.Key.SendType,
    ep.Key.Registry,
    TotalSent = ep.Count()
});

生成的IEnumerable<>将包含三个属性SendTypeRegistryTotalSent