2015-08-22 46 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,并给了我一个不同的错误。谢谢! –

相关问题