将SQL转换为等效的LINQ

本文关键字:LINQ SQL 转换 | 更新日期: 2023-09-27 18:09:03

是否有办法将下面的语法转换为LINQ等效。

select DateName( month , DateAdd( month , month(cour_date) , 0 ) - 1 ),count(*)
from course
where cour_date>CONVERT(varchar, YEAR(GETDATE())) + '-01' + '-01'
and cour_deleted is null and cour_type='Group'
group by month(cour_date)
order by month(cour_date)

将SQL转换为等效的LINQ

你可以这样写:

var query = from c in course 
            where c.cour_date > DateTime.Parse("2011-01-01") 
                 && c.cour_deleted == null && c.cour_type.Equals("Group") 
            orderby c.cour_date 
            group c by c.cour_date.Month

对于查询中的每个IGrouping<DateTime, course>,您都有Count值,并且您还可以格式化cour_date的DateTime值以转换为字符串mount