【问题标题】:LINQ: Group by month and year within a datetime fieldLINQ:在日期时间字段中按月和年分组
【发布时间】:2023-03-28 20:17:01
【问题描述】:

我有一张带有日期时间字段的表格。我想检索按月/年组合和该月/年中出现的记录数分组的结果集。这如何在 LINQ 中完成?

我能想到的最接近的是 TSQL:

select substring(mo,charindex(mo,'/'),50) from (
select mo=convert(varchar(2),month(created)) + '/' + convert(varchar(4), year(created)) 
 ,qty=count(convert(varchar(2),month(created)) + '/' + convert(varchar(4), year(created)))
from posts 
group by convert(varchar(2),month(created)) + '/' + convert(varchar(4), year(created))
) a
order by substring(mo,charindex(mo,'/')+1,50)

但我不会说这有效......

【问题讨论】:

    标签: linq tsql group-by substring


    【解决方案1】:
    var grouped = from p in posts
         group p by new { month = p.Create.Month,year= p.Create.Year } into d
         select new { dt = string.Format("{0}/{1}",d.Key.month,d.Key.year), count = d.Count() };
    

    这是LINQ 中可用的日期时间函数列表。为此,您还需要了解multi-column grouping

    降序排列

    var grouped = (from p in posts 
      group p by new { month = p.Create.Month,year= p.Create.Year } into d 
      select new { dt = string.Format("{0}/{1}",d.Key.month,d.Key.year), count = d.Count()}).OrderByDescending (g => g.dt);
    

    【讨论】:

    • 我想按月份排序,比如当前的第一个月在顶部,上个月在下一个,依此类推??请帮忙
    • 你可以通过添加 orderbydescending 来做到这一点
    【解决方案2】:

    这适用于那些试图完成相同但使用 lambda 表达式的人。

    假设您已经有一个实体集合,并且每个实体都将 OrderDate 作为其属性之一。

    yourCollection
    // This will return the list with the most recent date first.
    .OrderByDescending(x => x.OrderDate)
    .GroupBy(x => new {x.OrderDate.Year, x.OrderDate.Month})
    // Bonus: You can use this on a drop down
    .Select(x => new SelectListItem
            {
               Value = string.Format("{0}|{1}", x.Key.Year, x.Key.Month),
               Text = string.Format("{0}/{1} (Count: {2})", x.Key.Year, x.Key.Month, x.Count())
            })
    .ToList();
    

    如果您不需要 SelectListItem 的集合,那么只需将 select 替换为这个:

    .Select(x => string.Format("{0}/{1} (Count: {2})", x.Key.Year, x.Key.Month, x.Count()))
    

    【讨论】:

      【解决方案3】:

      你也可以这样做

      from o in yg
      group o by o.OrderDate.ToString("MMM yyyy") into mg
      select new { Month = mg.Key, Orders = mg }
      

      你的结果将是

      {2014 年 1 月,25} {Feb 2015, 15} 等...

      【讨论】:

      • 适用于对象,但不适用于将发送到数据库的IQueryable<>。在那里你得到Method 'System.String ToString(System.String)' has no supported translation to SQL.
      【解决方案4】:

      This Site 有一个可以满足您需求的示例。

      这是基本语法:

      from o in yg
      group o by o.OrderDate.Month into mg
      select new { Month = mg.Key, Orders = mg }
      

      【讨论】:

      • 如果列表包含多个年份的订单,这会将 2010 年 1 月、2011 年和 2012 年 1 月的订单归为同一组。
      【解决方案5】:

      这是一个在 DateTime 中分组的简单解决方案。

      List<leaveallview> lav = new List<leaveallview>();
      lav = dbEntity.leaveallviews.Where(m =>m.created==alldate).ToList();
      dynamic lav1 = lav.GroupBy(m=>m.created.Value.GetDateTimeFormats()).FirstOrDefault().ToList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 2013-04-05
        • 2017-10-17
        相关资源
        最近更新 更多