实体框架Where子句奇怪的行为

发布于 2024-12-15 06:43:37 字数 1996 浏览 0 评论 0原文

所以我正在使用 EF 并尝试进行一个非常简单的数据库调用。我对其他对象进行了相同的调用,但没有得到这种行为。行为是,如果我在 where 语句中链接两个子句,我将不再得到任何结果。这是一些示例代码:

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false && w.GadgetID == relatedGadgetID).ToList();
    // This does not get any of the objects in the database
}

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false).Where(w => w.GadgetID == relatedGadgetID).ToList();
    // This does not get any of the objects in the database
}

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    var x =  db.Widgets.Where(w => w.Deleted == false).ToList();
    return x.Where(w => w.GadgetID == relatedGadgetID).ToList();
    // This DOES!
}

为什么?

编辑:离开评论,我尝试了解决方法,但仍然不起作用。这是我尝试过的,我做错了什么吗?

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false && (relatedGadgetID == null ? w.GadgetID == null : w.GadgetID == relatedGadgetID)).ToList();
    // Still doesn't work =(
}

Edit2:尝试了另一个修复,仍然没有运气。

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => (w.Deleted == false) && (w.GadgetID == relatedGadgetID || (relatedGadgetID == null && w.GadgetID == null))).ToList();
    // Still doesn't work =(
}

Edit3:再次尝试了 Object.Equals,什么也没有。

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false && w.GadgetID.Equals(relatedGadgetID)).ToList();
    // Still doesn't work =(
}

So I am using EF and trying to make a pretty simple database call. I have made an identical call on other objects, and don't get this behavior. The behavior is that if I chain two clauses in my where statement, I no longer get any results. Here is some example code:

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false && w.GadgetID == relatedGadgetID).ToList();
    // This does not get any of the objects in the database
}

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false).Where(w => w.GadgetID == relatedGadgetID).ToList();
    // This does not get any of the objects in the database
}

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    var x =  db.Widgets.Where(w => w.Deleted == false).ToList();
    return x.Where(w => w.GadgetID == relatedGadgetID).ToList();
    // This DOES!
}

Why?

Edit: Going off of the comments, I tried the workaround, and it still didn't work. Here is what I tried, did I do something wrong?

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false && (relatedGadgetID == null ? w.GadgetID == null : w.GadgetID == relatedGadgetID)).ToList();
    // Still doesn't work =(
}

Edit2: Tried another fix, still no luck.

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => (w.Deleted == false) && (w.GadgetID == relatedGadgetID || (relatedGadgetID == null && w.GadgetID == null))).ToList();
    // Still doesn't work =(
}

Edit3: Tried the Object.Equals, again, nothing.

public List<Widgets> FindAll(long? relatedGadgetID)
using (myDBContext db = new myDBContext())
{
    return db.Widgets.Where(w => w.Deleted == false && w.GadgetID.Equals(relatedGadgetID)).ToList();
    // Still doesn't work =(
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-22 06:43:37

在我早期尝试修复它时,我不小心将类属性更改为不可为空,而表列仍然为空。当我意识到这一点时,最初的尝试仍然有相同的结果,但解决方法有效。我最终使用了第一个编辑解决方法。谢谢碎玻璃!

In my attempts to fix it early on, I accidentally changed my class property to not be nullable, while the table column still was. Once I realized this, the original attempts still had the same result, but the workaround worked. I ended up using that first edit workaround. Thanks Broken Glass!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文