简体   繁体   English

如何将带有group by子句的SQL select Count转换为LINQ?

[英]How to convert SQL select Count with group by clauses into LINQ?

How can I convert the following SQL query into LINQ? 如何将以下SQL查询转换为LINQ?

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

Please Help. 请帮忙。

Thanks 谢谢

give this a try, 试试看

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()
    }

Following is method syntax 以下是方法语法

 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()
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM