如何写这个linq [英] How to write this to linq

查看:101
本文介绍了如何写这个linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我用延伸方法写这个给linq吗?



对于resule,我只需要月份和计数(CreatedDate)值< br $>


Can anyone could help me write this to linq using extension method??

For the resule, I only need the month and the count(CreatedDate) value

select YEAR(CreatedDate),MONTH(CreatedDate),count(CreatedDate) as number 
from Accounts
group by YEAR(CreatedDate),MONTH(CreatedDate)

推荐答案

我不确定你是否意味着你不需要年份结果或如果您也不想按年份分组。所以这里有一个版本的年份和一个没有:

I'm not sure if you mean you don't need the year in the result or if you also don't want to group by the year. So here's one version with the year and one without:
var result1 = Accounts.GroupBy(a => new { Year = a.CreatedDate.Year, Month = a.CreatedDate.Month })
                      .Select(g => new { Year = g.Key.Year, Month = g.Key.Month, Count = g.Count() });
                    //.ToList();  //optionally

var result2 = Accounts.GroupBy(a => a.CreatedDate.Month)
                      .Select(g => new { Month = g.Key, Count = g.Count() });
                    //.ToList();  //optionally


除了 Sascha [ ^ ],您可以使用以下内容:



In addition to solution1 by Sascha[^], you can use something like this:

var result = Accounts.GroupBy(a => a.CreatedDate.ToString("yyyy-MM"))
                      .Select(g => new { Month = g.Key, Count = g.Count() });


这篇关于如何写这个linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆