我怎么能查询在实体框架空值? [英] How can i query for null values in entity framework?

查看:231
本文介绍了我怎么能查询在实体框架空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行这样的查询

   var result = from entry in table
                     where entry.something == null
                     select entry;

和获得 IS NULL 生成。

编辑: 前两个答案之后,我觉得有必要澄清,我使用实体框架 而不是LINQ to SQL的。该的Object.Equals()方法似乎并不在EF工作。

Edited: After the first two answers i feel the need to clarify that I'm using Entity Framework and not Linq to SQL. The object.Equals() method does not seem to work in EF.

编辑二: 上面的查询按预期工作。它正确生成 IS NULL 。然而,我的生产code是

Edit no.2: The above query works as intended. It correctly generates IS NULL. My production code however was

value = null;
var result = from entry in table
                         where entry.something == value
                         select entry;

和生成的SQL是东西= @p; @p = NULL 。看来,EF正确地转换不断前pression但如果一个变量是涉及它把它只是像一个正常的比较。有道理实际。我会关闭这个问题

and the generated SQL was something = @p; @p = NULL. It seems that EF correctly translates the constant expression but if a variable is involved it treats it just like a normal comparison. Makes sense actually. I'll close this question

推荐答案

解决方法的LINQ到SQL:

Workaround for Linq-to-SQL:

var result = from entry in table
             where entry.something.Equals(value)
             select entry;

解决方法对LINQ到实体(哎哟!):

Workaround for Linq-to-Entities (ouch!):

var result = from entry in table
             where (value == null ? entry.something == null : entry.something == value)
             select entry;

这是它咬了我好几次一个讨厌的错误。 <击>如果这个bug已经影响到你,请访问<一href="http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1015361-incorrect-handling-of-null-variables-in-where-cl?ref=title">bug对UserVoice的报告,并让微软知道这个缺陷已经影响到你。

This is a nasty bug which has bitten me several times. If this bug has affected you too, please visit the bug report on UserVoice and let Microsoft know that this bug has affected you as well.

编辑: <一个href="http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1015361-incorrect-handling-of-null-variables-in-where-cl?ref=title#suggestion-1015361">This错误是被固定在EF 4.5 !感谢大家upvoting这个错误!

This bug is being fixed in EF 4.5! Thanks everyone for upvoting this bug!

有关向后兼容性,它会选择在 - 你需要手动启用设置,使进入==值的工作。无字尚未对什么该设置。敬请期待!

For backwards compatibility, it will be opt-in - you need manually enable a setting to make entry == value work. No word yet on what this setting is. Stay tuned!

编辑2:据<一href="http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1015361-incorrect-handling-of-null-variables-in-where-cl">this帖子由EF团队,此问题已得到修复在EF6!呜呼!

我们改变EF6的默认行为,以弥补三值逻辑。

We changed the default behavior of EF6 to compensate for three-valued logic.

这意味着现有的code依赖于旧行为的空!= NULL ,但只是比较变量时)要么需要将其更改为不依赖于这种行为,或者设置<一个href="http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontextoptions.usecsharpnullcomparisonbehavior.aspx"><$c$c>UseCSharpNullComparisonBehavior假以使用破旧的行为。

This means that existing code that relies on the old behavior (null != null, but only when comparing to a variable) will either need to be changed to not rely on that behavior, or set UseCSharpNullComparisonBehavior to false to use the old broken behavior.

这篇关于我怎么能查询在实体框架空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆