简体   繁体   English

LINQ查询以按月将记录分组

[英]LINQ query to group records by month within a period

I am looking for some help on adapting the following LINQ query to return all dates within the next 6 months, even those where no records fall within the given month. 我正在寻求一些帮助,以适应以下LINQ查询以返回未来6个月内的所有日期,即使那些没有记录在给定月份内的日期也是如此。

var maxDate = DateTime.Now.AddMonths(6);

var orders = (from ord in db.Items

where (ord.Expiry >= DateTime.Now && ord.Expiry <= maxDate)

group ord by new
    {
        ord.Expiry.Value.Year,
        ord.Expiry.Value.Month
    }
into g
select new ExpiriesOwnedModel
    {
        Month = g.Select(n => n.Expiry.Value.Month).First(),
        Quantity = g.Count()
    }).ToList();

I'd really appreciate any assistance or pointers on how best to implement this. 我非常感谢您提供任何有关如何最好地实现这一目标的帮助或指点。

I'm not sure how well it'll interact with your database, but I'd do this as with a join: 我不确定它将如何与您的数据库进行交互,但是我可以像加入连接那样进行:

var firstDaysOfMonths = Enumerable.Range(0, 7).Select(i =>
    new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(i));
var orders = firstDaysOfMonths.GroupJoin(
    db.Items,
    fd => fd,
    ord => new DateTime(ord.Expiry.Value.Year, ord.Expiry.Value.Month, 1),
    (fd, ords) => new { Month = fd.Month, Quantity = ords.Count() });

Note you may end up with an extra month where before you didn't (on the first day of the month?) 请注意,您可能要多花一个月才能到,而之前却是一个月(在月的第一天?)

Stolen from Rawling's answer, if you prefer query syntax for group joins (I do): 如果您更喜欢组联接的查询语法(我愿意),则从Rawling的答案中被窃取:

var orders =
    from month in Enumerable.Range(0, 7)
        .Select(i => new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(i))
    join ord in db.Items
        on month equals new DateTime(ord.Expiry.Value.Year, ord.Expiry.Value.Month, 1)
        into ords
    select new { month.Month, Quantity = ords.Count() };

Alternative if it does not play nice with the database: 如果它不能很好地与数据库配合使用,则选择:

var rawGroups = db.Items.Where(item.Expiry >= DateTime.Now && ord.Expiry <= maxDate)
                        .GroupBy(item => new
                           {
                            item.Expiry.Value.Year,
                            item.Expiry.Value.Month
                           }, g => new ExpiriesOwnedModel()
                          {
                            Month = g.Key.Month,
                            Quantity = g.Count()
                          }).ToDictionary(model => model.Month);

var result = Enumerable.Range(DateTime.Now.Month,6)
                       .Select(i => i > 12 ? i - 12 , i)
                       .Select(i => rawGroups.Keys.Contains(i) ?
                                    rawGroups[i] :
                                    new ExpiriesOwnedModel() 
                                    { Month = i , Quantity = 0 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM