Reputation: 25
How can I convert the following SQL query into LINQ?
SELECT [EmpUserName],[LeaveTypeName],Count([NoOfDays]) as 'Count'
FROM [NSL.LeaveSystem02].[dbo].[ProcessedCPLeaves]
GROUP BY [LeaveTypeName],[EmpUserName],[ProcessingDate]
Please Help.
Thanks
Upvotes: 2
Views: 648
Reputation: 30698
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()
});
Upvotes: 2
Reputation: 263723
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()
}
Upvotes: 2