Linq JOIN 在比较器包含 DbNull 的强类型数据集上抛出异常
Linq JOIN on a strongly typed dataset where comparator contains DbNull throws exception
考虑查询:
Dim orgs = From g In dbDS.gi_game
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Select o
这将在 g.DeveloperID
为 DBNull 时引发异常(这是设计使然 - 无法将 DBNull 更改为任何其他值):
System.Data.StrongTypingException: 'The value for column 'DeveloperID' in table 'gi_game' is DBNull.'
我试过这个:
Dim orgs = From g In dbDS.gi_game
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Where Not g.IsDeveloperIDNull
Select o
但是在查询枚举时遇到同样的错误。我如何控制 dbnulls?
更新:
感谢 Shaybakov 在下面的回答,通过混合 linq 和 lambda 解决了问题。 vb.net代码:
Dim orgs = From g In dbDS.gi_game.Where(Function(x) x.IsDeveloperIDNull = False)
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Select o
c#语法
From g In dbDS.gi_game.Where(x=>!x.IsDeveloperIDNull())
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Select o
考虑查询:
Dim orgs = From g In dbDS.gi_game
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Select o
这将在 g.DeveloperID
为 DBNull 时引发异常(这是设计使然 - 无法将 DBNull 更改为任何其他值):
System.Data.StrongTypingException: 'The value for column 'DeveloperID' in table 'gi_game' is DBNull.'
我试过这个:
Dim orgs = From g In dbDS.gi_game
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Where Not g.IsDeveloperIDNull
Select o
但是在查询枚举时遇到同样的错误。我如何控制 dbnulls?
更新:
感谢 Shaybakov 在下面的回答,通过混合 linq 和 lambda 解决了问题。 vb.net代码:
Dim orgs = From g In dbDS.gi_game.Where(Function(x) x.IsDeveloperIDNull = False)
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Select o
c#语法
From g In dbDS.gi_game.Where(x=>!x.IsDeveloperIDNull())
Join o In dbDS.gi_organisation On g.DeveloperID Equals o.ID
Select o