我正在使用VB.NET和MySQL在MVC2中进行开发,尝试将简单的SQL查询转换为LINQ时遇到问题。
SQL查询:
SELECT Location_Number, sum(Column1) as totalC1, sum(Column2) as totalC2 FROM MyTable WHERE year = 2010 and month = 8 GROUP BY Location_Number ORDER BY Location_Number
LINQ查询:
From r In MyTable _ Where r.Year = 2010 And r.Month = 8 _ Group r By LN = r.Location_Number Into l = Group _ Order By LN _ Select New With { _ .Location_Number = LN, _ .DepositCount = l.Sum(Function(r) r.Column1), _ .OtherCount = l.Sum(Function(r) r.Column2) _ }
生成的错误是:
执行命令定义时发生错误。有关详细信息,请参见内部异常。
内部异常是:
“字段列表”中的未知列“ GroupBy1.K1”
这是LINQ生成的SQL:
SELECT `Project1`.`Location_Number`, `Project1`.`C1`, `Project1`.`C2` FROM ( SELECT `GroupBy1`.`A1` AS `C1`, `GroupBy1`.`A2` AS `C2`, `GroupBy1`.`K1` AS `Location_Number` FROM ( SELECT Sum(`Column1`) AS `A1`, Sum(`Column2`) AS `A2` FROM `MyTable` AS `Extent1` WHERE (`Extent1`.`Year` = @p__linq__0) AND (`Extent1`.`Month` = @p__linq__1) GROUP BY `Extent1`.`Location_Number` ) AS `GroupBy1` ) AS `Project1` ORDER BY `Location_Number` ASC
查看该查询,很容易找出导致错误的原因。简而言之,最内部的查询仅返回2列,而其上方的查询试图选择SELECT3,因此出现“未知列错误”。那么为什么会这样呢?我的LINQ查询出了什么问题?
谢谢
这是一个MySQL连接器错误:http : //bugs.mysql.com/bug.php?id=46742
在最新版本(6.3.5)中未修复。