I'm new to Linq and had to take over a former colleagues project. I've been searching, but I am still a bit stumped on this problem...
I have the following code:
var q = db.v_QCData.OrderByDescending(e => e.DataID).Take(MAX_WAFER_LIST);
QcWaferList = null;
if (q != null)
{
QcWaferList = q.ToList();
were db refers to a data model. This works great some of the time, so I know the model is fine, but at other times at ToList(), I get Object reference not set to an Instance of an object. I can't understand why it should be empty some of the time and not others. So I tried this:
bool has = db.v_QCData.Any();
That returns true, but what I can't understand is how it can have data in it, but not return any data. Any advice at all is appreciated.
EDIT: line 162 is q.ToList();
Stack Trace: at System.Data.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)\r\n at System.Data.EntityKey.GetHashCode()\r\n at System.Collections.Generic.GenericEqualityComparer
1.GetHashCode(T obj)\r\n at System.Collections.Generic.Dictionary
2.FindEntry(TKey key)\r\n at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value)\r\n at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry)\r\n at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func
2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)\r\n at lambda_method(Closure , Shaper )\r\n at System.Data.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper shaper)\r\n at System.Data.Common.Internal.Materialization.Shaper
1.SimpleEnumerator.MoveNext()\r\n at System.Collections.Generic.List1..ctor(IEnumerable
1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n at Solar1366Tech.LaserQCHMI.ViewModels.MachineViewModel.UpdateWaferList() in c:\TFS2012\Direct Wafer\DWWF03\Solar1366Tech.LaserQCHMI\Solar1366Tech.LaserQCHMI\ViewModels\MachineViewModel.cs:line 162