开发者

Help with LINQ group by

开发者 https://www.devze.com 2023-02-24 14:13 出处:网络
I\'m trying to replicate this SQL query SELECTCOUNT(1) AS Records, MONTH(date) AS Month, YEAR(date) AS YEAR

I'm trying to replicate this SQL query

SELECT     COUNT(1) AS Records, MONTH(date) AS Month, YEAR(date) AS YEAR
FROM         tblBlogEntries
GROUP BY MONTH(d开发者_如何学Goate), YEAR(date)
ORDER BY year DESC, month DESC

Into LINQ, I've gotten as far as this:

var q = from Rec in db.tblBlogEntries
        group Rec by new { Rec.date.Value.Year, Rec.date.Value.Month } into G
        select new {
            Month = G.Key.Month,
            Year = G.Key.Year
        };

But I'm still new to LINQ and am a little lost. Thanks for any help!


Try this instead:

var query = from rec in db.tblBlogEntries 
            group rec by new { rec.date.Value.Month,
                               rec.date.Value.Year } into g
            orderby g.Key.Year descending, g.Key.Month descending
            select new {
                Count = g.Count(),
                g.Key.Year,
                g.Key.Month
            };
0

精彩评论

暂无评论...
验证码 换一张
取 消