在Linq for Entity Framework中将SQL组转换为等效项

时间:2011-10-17 15:12:28

标签: linq entity-framework entity-framework-4 linq-to-entities

我使用EF 4和c#,MS SQL 2008。

我有一个T-SQL:

select COUNT(*)
from CmsContents
where IsPublished = 1 and isDeleted = 0
GROUP BY Day(Date)

我需要将它转换为EF,但我无法得到它。

我正在使用的是:

            var contentsByMonth = context.CmsContents
                .Where(c => c.IsPublished == true & c.IsDeleted == false)
                .GroupBy(c => c.Date.Month).ToList().Count();
你能举个例子吗?感谢

1 个答案:

答案 0 :(得分:1)

var contentsByMonth = context.CmsContents
                .Where(c => c.IsPublished == true & c.IsDeleted == false)
                .GroupBy(c => c.Date.Day)
                .Select(g => new {Day = g.Key, Counts = g.Count()})
                .ToList();