2011-07-05 53 views
0

我不斷收到下面的代碼錯誤。它說Error "Operator '==' cannot be applied to operands of type 'string' and 'int'.我看過一段視頻,Julie Lerman也完全一樣。她雖然沒有收到錯誤。爲什麼?簡單的C#Linq to Entities查詢不起作用

private static void CustomerQuery() 
{ 
    var context = new NorthwindEntities(); 
    var query = from c in context.Customers 
       where c.CustomerID == 5 
       select c; 

    var customers = query.FirstOrDefault(); 
} 
+4

因爲CustomerID是數據庫表中的字符串(varchar)? –

+2

您確定Customer.CustomerID是一個i​​nt而不是一個字符串嗎? –

+0

CustomerID是一個整數嗎?如果不是那麼當然這不會工作,錯誤告訴你這個問題,你不能直接比較一個字符串和一個整數。 – hdougie

回答

1

檢查CustomerID的數據類型。否則將它們轉換爲int。

+0

明白了。謝謝。 – James

0

改爲使用where c.CustomerID == "5"

+1

它是c#中字符串的雙引號# –

+0

應該不是'where c.CustomerID ==「5」?'即雙引號而不是單引號。 – hdougie

+0

你好,我同意剛剛編輯的 –

1

完蛋了,檢查客戶ID的類型表,如果它是字符串查詢更改爲

private static void CustomerQuery() 

     { 
      var context = new NorthwindEntities(); 
      var query = from c in context.Customers 
         where c.CustomerID == "5" 
         select c; 

      var customers = query.FirstOrDefault(); 
     } 
+0

明白了。謝謝。 – James

0

似乎CustomerIDint類型的不行。 Typecast它到intint datatype進行比較。

+0

明白了。謝謝。 – James