2010-09-16 97 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