所以我使用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 =(
}
答案 0 :(得分:0)
在我试图尽早修复它时,我意外地将我的类属性更改为不可为空,而表列仍然是。一旦我意识到这一点,原始尝试仍然有相同的结果,但解决方法工作。我最终使用了第一个编辑解决方法。谢谢碎玻璃!