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

Bri*_*ton 7 c# linq sql-server linqpad linq-to-sql

我已经查看了各种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 };
Run Code Online (Sandbox Code Playgroud)

错误:

Type inference failed in the call to Join
Run Code Online (Sandbox Code Playgroud)

现在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 ... 
Run Code Online (Sandbox Code Playgroud)

Tom*_*kel 7

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

Convert.ToInt32(column)  should work in linqpad and your c# application just fine.
Run Code Online (Sandbox Code Playgroud)

(我用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();
Run Code Online (Sandbox Code Playgroud)

更新:

每个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();
Run Code Online (Sandbox Code Playgroud)