1

它编译正常,但是当我尝试遍历 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是错误的?

4

2 回答 2

3

试试这个:

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)
             };
于 2012-07-10T22:38:43.073 回答
1

我猜这是因为 in 中的dataPrice是 String 或除DateTime.

尝试将(DateTime)c.data)演员改为 a Convert.ToDateTime(c.data)

我不确定 Linq-SQL 翻译器是否支持强制转换。

如果可能,将数据库中的基础数据类型更改为 a DateTimeif 它存储日期时间值。

于 2012-07-10T22:37:21.000 回答