datetime和groupby

ix0qys7i  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(406)

我正在使用连接到mysql的linqpad5。正在尝试运行以下表达式:

Productions.Where(p => p.Datetime.Year == 2018).GroupBy(p => p.Amount)

结果是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?.datetime)

linqpad将表达式转换为以下sql语句:

SELECT t0.amount, YEAR(A6139838?.datetime) AS c0
FROM productions AS t0
WHERE (YEAR(t0.datetime) = 2018)
GROUP BY t0.amount

问题来自访问“.year”,

.GroupBy(p => p.Datetime)  //<-- this works
.GroupBy(p => p.Datetime.Year) //<--- MysqlException

截图:

0tdrvxhp

0tdrvxhp1#

试试这个
方法1:

.GroupBy(p => p.Datetime != null ? (int)p.Datetime.Year : 0)

方法2:
将datetime的属性名更改为其他名称。实际上名称与sql数据类型相同。可能会有冲突

相关问题