Linq查询类型推断在对Join的调用中失败

时间:2015-08-22 03:49:25

标签: c# sql-server linq linq-to-sql linqpad

我已经查看了各种stackoverflow的答案,但我没有看到修复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 ... 

1 个答案:

答案 0 :(得分:7)

问题是你不能连接不同类型的表列值!

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

(我用parens包裹并添加了ToList())

如果group是string并且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();

<强>更新

每个OP,我同意他的意思,它应该将其他值转换为字符串

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