c# - group by 操作包含无法翻译的表达式

标签 c# linq

它编译正常但是当我尝试遍历 LINQ 查询的结果时我遇到了这样的异常 The group by operation contains an expression that cannot be translated 查询是

var query0 = from c in dc.Prices
              where Convert.ToDateTime(c.data).CompareTo(left) >= 0
              && Convert.ToDateTime(c.data).CompareTo(right) <= 0
              && c.idsticker.Equals(x)
            group c by new { ((DateTime)c.data).Year, ((DateTime)c.data).Month }
                  into groupMonthAvg
                  select new
                  {
                      years = groupMonthAvg.Key.Year,
                      months = groupMonthAvg.Key.Month,
                      prices = groupMonthAvg.Average(i => i.value)
                  };

group by 函数中哪个表达式是错误的?

最佳答案

试试这个:

var query0 = from c in dc.Prices
             let date = Convert.ToDateTime(c.data)
             where date.CompareTo(left) >= 0 && date.CompareTo(right) <= 0 && c.idsticker.Equals(x)
             group c by new { date.Year, date.Month } into groupMonthAvg
             select new
             {
                 years = groupMonthAvg.Key.Year,
                 months = groupMonthAvg.Key.Month,
                 prices = groupMonthAvg.Average(i => i.value)
             };

关于c# - group by 操作包含无法翻译的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11423008/

相关文章:

c# - “does not include” 的 Json.NET JsonPath 语法,还是否定匹配?

c# - 如何阻止 ReSharper 在分配的变量和链式方法调用之间插入不必要的换行符

c# - 根据动态键获取重复数据

c# - 如何使用 linq 进行计数和分组

c# - Quickbooks .IIF 文件发票

c# - 如何在 operator== 方法中检查 null?

c# - 从客户端调用代码隐藏(安全页面)中的方法(JavaScript)

c# - 如何使用 LINQ group by with where 子句

c# - 嵌套列表上的 Linq - 选择所有 ID

c# - 如何使用 LINQ 执行单词搜索?