Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Linq-前12个月按日期时间分组-包括空月_Linq_Datetime_Group By - Fatal编程技术网

Linq-前12个月按日期时间分组-包括空月

Linq-前12个月按日期时间分组-包括空月,linq,datetime,group-by,Linq,Datetime,Group By,我有一个场景,需要检索按datetime字段的月份分组的对象计数 我找到了下面的帖子,这让我有了一段路 …但我需要列出从今天开始的前12个月以及每个月的物品数量,这正是我努力的方向 我也看到过其他一些类似问题/解决方案的帖子,但我选择了上面的一篇,因为这也是一个要求,任何月份都要生成一个计数为0的记录 谢谢你对我的帮助 编辑 好的,多亏了谜(Enigmativity)(多谢您花时间!),我才有了进一步的了解: …以及以下内容 public class NewsCountCollection {

我有一个场景,需要检索按datetime字段的月份分组的对象计数

我找到了下面的帖子,这让我有了一段路

…但我需要列出从今天开始的前12个月以及每个月的物品数量,这正是我努力的方向

我也看到过其他一些类似问题/解决方案的帖子,但我选择了上面的一篇,因为这也是一个要求,任何月份都要生成一个计数为0的记录

谢谢你对我的帮助

编辑

好的,多亏了谜(Enigmativity)(多谢您花时间!),我才有了进一步的了解:

…以及以下内容

public class NewsCountCollection
{
    public DateTime Month { get; set; }
    public string Title { get; set; }

    public NewsCountCollection(DateTime date, string title)
    {
        this.Month = new DateTime(date.Year, date.Month, 1);
        this.Title = title;
    }

}

public class NewsCountMonthList
{
    public string Month { get; set; }
    public int Count { get; set; }

    public NewsCountMonthList(string month, int count)
    {
        this.Month = month;
        this.Count = count;
    }
}

…看起来效率很低…但我忍不住想一定有比这更好的方法。我走对了吗?

这应该可以帮到你:

var now = DateTime.Now;
var last = new DateTime(now.Year, now.Month, 1);
var first = last.AddMonths(-12);

var query =
    from s in somethings
    where s.DateTimeField >= first
    where s.DateTimeField < last
    select new
    {
        Month = new DateTime(s.DateTimeField.Year, s.DateTimeField.Month, 1),
        Something = s,
    };

var lookup = query.ToLookup(x => x.Month, x => x.Something);

var counts =
    from n in Enumerable.Range(-12, 12)
    let Month = last.AddMonths(n)
    select new
    {
        Month,
        Count = lookup[Month].Count(),
    };
var now=DateTime.now;
var last=新日期时间(现在.年,现在.月,1);
var first=最后一个月的月数(-12);
变量查询=
从某种意义上说
其中s.DateTimeField>=第一个
其中s.DateTimeFieldx.Month,x=>x.Something);
var计数=
从可枚举范围(-12,12)中的n开始
let Month=last.AddMonths(n)
选择新的
{
月,
Count=查找[Month]。Count(),
};

您可能需要稍微修改一下,但结构应该是合理的。

请发布您尝试过的代码、正在处理的对象结构以及任何示例数据。
var now = DateTime.Now;
var last = new DateTime(now.Year, now.Month, 1);
var first = last.AddMonths(-12);

var query =
    from s in somethings
    where s.DateTimeField >= first
    where s.DateTimeField < last
    select new
    {
        Month = new DateTime(s.DateTimeField.Year, s.DateTimeField.Month, 1),
        Something = s,
    };

var lookup = query.ToLookup(x => x.Month, x => x.Something);

var counts =
    from n in Enumerable.Range(-12, 12)
    let Month = last.AddMonths(n)
    select new
    {
        Month,
        Count = lookup[Month].Count(),
    };