2015-08-22 16 views
6

我已經看過各種計算器的答案,但我沒有看到用linq修復我的連接的方式。Linq查詢類型推斷失敗,在加入調用

2表

var query = from tips in TblTips 
      where tips.Id == 30 
      join files in TblFiles on tips.Id equals files.Group 
      select new { tips, files }; 

錯誤:

Type inference failed in the call to Join 

現在tips.Id是int,而files.Group是一個varchar

我試圖做.value的

tips.id.Value  --> the word Value not working (most recent linqpad) 

(int)files.Group --> it doesn't like that ... 

回答

6

問題是你cann不連接不同類型的表列值!

Convert.ToInt32(column) should work in linqpad and your c# application just fine. 

(我裹在括號,並增加了ToList())

這應該爲你工作,如果組串和id爲int

var query = (from tips in TblTips 
      where tips.Id == 30 
      join files in TblFiles on tips.Id equals Convert.ToInt32(files.Group) 
      select new { tips, files }).ToList(); 

UPDATE:

每OP的

,我同意他應該將其他值轉換爲字符串

​​
+1

我認爲如果'files.Group''可以是一個整數以外的東西,''tips.Id.ToString()等於files.Group''實際上會更安全。 – kmc059000

+0

好吧,這個工程,我曾嘗試做一個Int32.Parse,並給了我一個不同的錯誤。謝謝! –