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 =(
}