如何将此SQL脚本转换为LINQ?并保持计数()?

如何将此SQL脚本转换为LINQ?并保持计数()?,sql,asp.net,sql-server,vb.net,linq,Sql,Asp.net,Sql Server,Vb.net,Linq,我已经制作并测试了一个运行完美的MS SQL脚本。我一直在缓慢地过渡到LINQ,在将脚本转换为VB.Net和LINQ时遇到了困难 SELECT DATEPART(dd,Generated) AS Day, count(ID) as Total FROM Alarm WHERE Generated >=CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE()),0),120) GROUP BY DATEPART(dd,Gene

我已经制作并测试了一个运行完美的MS SQL脚本。我一直在缓慢地过渡到LINQ,在将脚本转换为VB.Net和LINQ时遇到了困难

SELECT DATEPART(dd,Generated) AS Day, count(ID) as Total
FROM Alarm
WHERE Generated >=CONVERT(VARCHAR(10),DATEADD(MONTH, DATEDIFF(MONTH,0,GETDATE()),0),120)
GROUP BY DATEPART(dd,Generated)
基本上,我要求表格给出每月每天分组的行总数。在这个月的未来几天里,如果能将它放在一个0的位置,那就太棒了。任何关于如何将count转换为LINQ的建议都是非常好的

我的代码:

Dim ChartData = (From a In db.Alarms _
Where a.Generated >= FirstDayOfMonth(DateTimeNow) And a.Generated <= LastDayOfMonth(DateTimeNow) _
Group By Day = a.Generated Into Grp = Group, Count() _
Select Grp)

'Get the first day of the month
Public Function FirstDayOfMonth(ByVal sourceDate As DateTime) As DateTime
    Return New DateTime(sourceDate.Year, sourceDate.Month, 1)
End Function

'Get the last day of the month
Public Function LastDayOfMonth(ByVal sourceDate As DateTime) As DateTime
    Dim lastDay As DateTime = New DateTime(sourceDate.Year, sourceDate.Month, 1)
    Return lastDay.AddMonths(1).AddDays(-1)
End Function
Dim ChartData=(从数据库中的a开始)报警_

其中a.Generated>=FirstDayOfMonth(DateTimeNow)和a.Generated=FirstDayOfMonth(DateTimeNow)和a.Generated可以这样做。(我认为
dd
意味着天…`)


您可能无法在sql中按
x.Generated.Day
分组,如果是这样,请在
Where()之后添加
.AsEnumerable()
在内存中进行分组。

我有,但由于语法问题,我无法将其编译。我想问一下人们是如何使用count的,并从中学习。你的
中的
非常复杂。你只是想把120个月添加到现在吗?那你为什么不发布你的代码并寻求语法问题方面的帮助?我的目标是获得当月的当日范围。发布了我的代码,但我觉得这误导了我的真正目标。
        Dim ChartData = (From a In db.Alarms _
        Where a.Generated >= FirstDayOfMonth(DateTimeNow) And a.Generated <= LastDayOfMonth(DateTimeNow) _
        Group a By CalendarDate = a.Generated.Value.Date Into g = Group _
        Select New With {CalendarDate, .AlarmCount = g.Count()})
var myDate = new DateTime(); //calculate your date...
Alarms
    .Where(x => x.Generated >= myDate)
    .GroupBy(x => x.Generated.Day)
    .Select (x => new {
        Day = x.Key,
        Total = x.Count()
    }