【发布时间】:2013-02-02 08:59:36
【问题描述】:
我有一个带有“Clicks”表的 MySQL 数据库。有一个“已创建”列(日期时间),我想对其进行分组并选择年、月和日部分。我想统计特定日期范围(startDate 和 endDate)内每天的记录。
var query = from c in scope.Entities.Clicks
where c.Created >= startDate && c.Created <= endDate
group c by new {c.Created.Year, c.Created.Month, c.Created.Day}
into grouped
select new {
Year = grouped.Key.Year,
Month = grouped.Key.Month,
Day = grouped.Key.Day,
Clicks = grouped.Count()
};
这会产生错误的查询:
SELECT
`GroupBy1`.`K1` AS `C1`,
`GroupBy1`.`K2` AS `C2`,
`GroupBy1`.`K3` AS `C3`,
`GroupBy1`.`K4` AS `C4`,
`GroupBy1`.`A1` AS `C5`
FROM (SELECT
COUNT(1) AS `A1`
FROM `Click` AS `Extent1`
WHERE (`Extent1`.`Created` >= @p__linq__0) AND (`Extent1`.`Created` <= @p__linq__1)
GROUP BY
1,
YEAR(`Extent1`.`Created`),
MONTH(`Extent1`.`Created`),
DAY(`Extent1`.`Created`)) AS `GroupBy1`
出现错误:MySql.Data.MySqlClient.MySqlException: Can't group on 'A1'
我做错了什么?或者这是一个 MySql 连接器错误?我尝试了 MySQL 连接器 6.5.4 和 6.6.5
【问题讨论】:
-
你试过不使用
into grouped -
你到底是什么意思?如果您使用 group by...,则 'into' 不是可选的...
-
您的 linq 查询的结构似乎不正确,我将发布一个示例说明我会做什么......
-
Bas有关如何使用into的更好示例,请查看此链接stackoverflow.com/questions/14061718/… -
谢谢,我把答案放在原帖里了
标签: c# mysql linq entity-framework mysql-connector