Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL到LINQ-滚动月份日期_C#_Sql Server_Linq - Fatal编程技术网

C# SQL到LINQ-滚动月份日期

C# SQL到LINQ-滚动月份日期,c#,sql-server,linq,C#,Sql Server,Linq,希望将其转换为LINQ select COUNT(*) as 'BillsOver30' from pssuite_web.pujhaccd where billdays>30 and DATEDIFF(month,'07-07-2016', GETDATE()) <= 13 Group By Month(billdate) 从pssuite_web.pujhaccd中选择COUNT(*)作为“BillsOver30” 其中billdays>30,DATEDIFF(月'07-07

希望将其转换为LINQ

select COUNT(*) as 'BillsOver30' from pssuite_web.pujhaccd 
where billdays>30 and DATEDIFF(month,'07-07-2016', GETDATE()) <= 13
Group By Month(billdate)
从pssuite_web.pujhaccd中选择COUNT(*)作为“BillsOver30”
其中billdays>30,DATEDIFF(月'07-07-2016',GETDATE())30
&&m.billdate>=最早的日期

&&m.billdate要获得您在评论中描述的结果:

var result = (from m in DataContext.pujhaccds 
              where m.billdays > 30 && 
                  m.billdate >= earliestDate && 
                  m.billdate <= objdate1.DateStart 
              group m by m.billdate.Month into p   
              select new { Month = p.Key, Count = p.Count() }).ToList();
但是,如果您有来自不同年份的数据并希望将其分开,那么这种分组将是有问题的。如果这是按2个字段划分的案例组-年和月:

var result = (from m in DataContext.pujhaccds 
              where m.billdays > 30 && 
                  m.billdate >= earliestDate && 
                  m.billdate <= objdate1.DateStart 
              group m by new { m.billdate.Value.Month, m.billdate.Value.Year }  into p   
              select new { Date = $"{p.Key.Month} - {p.Key.Year}", Count = p.Count() }).ToList();
var result=(来自DataContext.pujhaccds中的m)
其中m.billdays>30&&
m、 billdate>=最早日期和

m、 billdate“不工作”如何?抛出一个错误,没有结果,错误的结果…?错误的结果。我只得到一个结果。我想要:7月-100日,6月-200日,5月-182日-13个月。sql不应该
选择billdate,Count(*)作为“BillsOver10”
?否则您只会得到一组计数,而不知道它们属于哪一组。另外,为什么在
DateDiff
中有硬编码日期,而不是假定的
billdate
?您是按
billdays
分组的,而不是
Month(billdate)
除了错误的分组,您还对组数进行了计数,而不是对每个组进行计数,因此您希望
选择p.count()
是的,我也会有不同年份的数据。您如何对这两个数据进行分组?@StemStep-检查我的编辑(并告诉我第二个选项是否有效-无法测试它,而不是在计算机旁边)@StemStep-ok已测试并有效。第二种格式将产生类似“August-2016”方法“System.String ToString(System.String)”的值'不支持转换为SQL@StemStep-啊,好的…它是在数据库中执行的…这就是为什么…正确的原因..然后只使用2列的分组-只是不给它添加日期,以不执行
ToString(string)
select p.Count()
var result = (from m in DataContext.pujhaccds 
              where m.billdays > 30 && 
                  m.billdate >= earliestDate && 
                  m.billdate <= objdate1.DateStart 
              group m by new { m.billdate.Value.Month, m.billdate.Value.Year }  into p   
              select new { Date = $"{p.Key.Month} - {p.Key.Year}", Count = p.Count() }).ToList();