I have this code :
IList<MyObject> myObjects = new List<MyObject>();
if (param != null)
{
myObjects = (from ... LINQ1 ...).ToList();
}
else
{
myObjects = (from ... LINQ2 ...).ToList();
}
foreach (MyObject myObject in myObjects)
{
}
when the foreach start, I get a System.NullReferenceException. Why? And how can I fix it? Looks strange…
Your object is being overwritten by one of the LINQ queries which is most likely returning a null value.
Also is myObjects meant to be an
IList<MyObject>orList<MyObject>?