【问题标题】:LINQ group-by and then display datetime for (dd mmm)LINQ group-by,然后显示(dd mmm)的日期时间
【发布时间】:2013-07-02 19:52:52
【问题描述】:

我希望 Linq 按日期分组,但以文本显示

这是我的代码

var groups = _uow.Orders.GetAll()
            .Where(x => x.Created > baselineDate)
            .GroupBy(x => x.Created.ToString("yyyy-MM-dd"));

var orders = new
        {
            Day = groups.Select(g => g.Key).ToArray(),
            Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
        };

结果是(不好放入图的标签)

 {"Day": [2, 3, 4, 5], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }

但我想要这个(每月)

"Day": ['Jan', Feb', 'Mar', 'Apr'], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }

或每天

"Day": ['Jan 1', 'Jan 2', 'Jan 3', 'Jan 4'], "Total": [9999.00, 9999.00, 9999.00, 9999.00] }

请为我可以玩的 DateTime.ToString() 提供建议。

谢谢大家。

【问题讨论】:

    标签: c# linq entity-framework datetime group-by


    【解决方案1】:

    感谢大家的帮助。 我找到了答案。

    添加

    .AsEnumerable()
    

     .GroupBy(x => x.Created.ToString("MMM d"));
    

    这里是完整代码

    var groups = _uow.Orders.GetAll()
                .Where(x => x.Created > baselineDate)
                .AsEnumerable()
                .GroupBy(x => x.Created.ToString("MMM d"));
    
            var orders = new
            {
                Day = groups.Select(g => g.Key).ToArray(),
                Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
            };
    

    【讨论】:

      【解决方案2】:

      This question 描述了如何将日期时间转换为月份的字符串名称。

      编辑:对于 EF,我们必须在执行任何字符串逻辑之前将所有内容都拉入内存:

      var orders = _uow.Orders.GetAll()
          .Where(x => x.Created > baselineDate)
          // pull into memory here, since we won't be making the result set any smaller and
          // we want to use DateTime.ToString(), which EF won't compile to SQL
          .AsEnumerable()
          // this will group by the whole date. If you only want to group by part of the date,
          // (e. g. day or day, month), you could group by x => x.Date.Month or the like
          .GroupBy(x => x.Date)
          .Select(g => new {
              // or use just "MMM" for just the month
              Dates = g.Select(g => g.Key.ToString("MMM d")).ToArray(),
              Total = ...
          });
      

      【讨论】:

      • 是的,我知道如何使用 ToString("MMMM") 或任何其他 stringFormat。但是当我使用 LINQ 分组时它不起作用。
      • @riseres 你在使用 linq 到对象吗? LINQ转SQL?英孚? NHibernate?
      • 非常感谢。 @ChaseMedallion,但我在代码中遇到错误。 g.**Select**(g => g.**Key**.ToString("MMM d")).ToArray() 怎么解决?
      • 它显示 this==> ExceptionMessage":"LINQ to Entities 无法识别方法 'System.String ToString(System.String)' 方法,并且此方法无法转换为存储表达式。 "
      • @riseres:试试我更新的代码。我为 EF 添加了必要的 AsEnumerable() 行
      【解决方案3】:

      你需要使用EF可以理解并翻译成SQL的函数,比如EntityFunctions.TruncateTime。这是您的更新代码:

      var groups = _uow.Orders.GetAll()
                  .Where(x => x.Created > baselineDate)
                  .GroupBy(x => EntityFunctions.TruncateTime(x.Created));
      

      然后在输出中使用字符串格式,但请确保您在 值已返回到 .Net 之后执行此操作,使用 AsEnumerable 切换到 Linq-to-Objects :

      var orders = new
              {
                  Day = groups.Select(g => g.Key)
                              .AsEnumerable()
                              .Select(x => x.ToString("MMM d"))
                              .ToArray(),
                  Total = groups.Select(g => g.Sum(t => t.Total)).ToArray(),
              };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-28
        • 2015-12-18
        相关资源
        最近更新 更多