2010-09-16 102 views
2

我正在使用VB.NET和MySQL在MVC2中开发,并遇到一个问题,试图将简单的SQL查询转换为LINQ。LINQ生成错误的查询,错误未知的列(VB.NET MySQL)

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列,而上面的查询正在尝试SELECT 3,因此出现未知列错误。那么为什么会这样呢?我的LINQ查询出了什么问题?

谢谢

+0

调整你的标题,希望你不介意。 – Larsenal 2010-09-16 20:09:58

+0

没有,任何可以帮助我得到答案的东西都会受到赞赏。 – nisav 2010-09-17 13:22:15

+0

从我的简短浏览这看起来像一个与MySQL连接器的错误。我有同样的问题,但当我删除子查询,在我的情况下,你的大小写总和它工作正常。让我知道你是否知道这一点。 – 2010-10-14 20:17:12

回答

2

这是一个MySQL连接错误:http://bugs.mysql.com/bug.php?id=46742

是不是固定在上(6.3.5)版本。

+0

任何人都有关于此问题的更新? – Remy 2011-02-01 14:13:52

+0

@Remy,Oracle团队尚未修复该错误,但您可以通过第三方提供商(例如DevArt) – SiberianGuy 2011-02-02 06:01:00

0

是的,这是一个mySQL连接器错误。

试试这个:

Using context = New YourContext() 
    Dim sql = "SELECT Location_Number, sum(Column1) as totalC1, sum(Column2) as totalC2 FROM MyTable WHERE year = @YEAR and month = @MONTH GROUP BY Location_Number ORDER BY Location_Number" 
    Dim args = New DbParameter() 
    { 
     New SqlParameter() With {Key .ParameterName = "YEAR", Key .Value = 2010}, New SqlParameter() With {Key .ParameterName = "MONTH", Key .Value = 8} 
    } 
    Return context.ExecuteStoreQuery(Of MyTable)(sql, args).ToList() 
End Using