如何将SQL select Count与group by子句转换为LINQ



如何将以下SQL查询转换为LINQ?

SELECT [EmpUserName],[LeaveTypeName],Count([NoOfDays]) as 'Count'
FROM [NSL.LeaveSystem02].[dbo].[ProcessedCPLeaves]
GROUP BY [LeaveTypeName],[EmpUserName],[ProcessingDate]

请帮助。

谢谢

方法语法

 ProcessedCPLeaves
.GroupBy(item => new { 
                 LeaveTypeName = item.LeaveTypeName,
                 EmpUserName = item.EmpUserName,
                 ProcessingDate = item.ProcessingDate
}).Select (grouping => new {
                 EmpUserName =grouping.Key.EmpUserName,
                 LeaveTypeName = grouping.Key.LeaveTypeName,
                 TotalCount= grouping.Count()
});

试一试,

from a in ProcessedCPLeaves
group a by new 
    {
        LeaveTypeName = a.LeaveTypeName,
        EmpUserName = a.EmpUserName,
        ProcessingDate = a.ProcessingDate
    } into g
select new
    {
        EmpUserName = g.Key.EmpUserName
        LeaveTypeName = g.Key.LeaveTypeName
        TotalCount = g.Count()
    }

相关内容

  • 没有找到相关文章

最新更新