【问题标题】:Linq JOIN on a strongly typed dataset where comparator contains DbNull throws exception在比较器包含 DbNull 的强类型数据集上的 Linq JOIN 抛出异常
【发布时间】:2021-06-18 17:10:48
【问题描述】:

考虑查询:

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#标签换成vb.net吗?
  • 您可以添加 where 子句来排除这些记录
  • 如果您想回答自己的问题,请发布答案,而不是将答案编辑到问题中。你也会得到赞成票..
  • 公平地说,Shaybakov 回答了这个问题 - 这就是我将他标记为答案的原因。

标签: .net linq join dataset strongly-typed-dataset


【解决方案1】:

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

【讨论】:

  • 太棒了——谢谢。我从来没有想过将 Linq 与 Lambda 混合使用。这开辟了一个全新的可能性世界!我已将 vb.net 代码放入我的 OP 中。
猜你喜欢
  • 2021-09-03
  • 2010-11-30
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
相关资源
最近更新 更多