1

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.GenericEqualityComparer1.GetHashCode(T obj)\r\n at System.Collections.Generic.Dictionary2.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](Func2 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.Shaper1.SimpleEnumerator.MoveNext()\r\n at System.Collections.Generic.List1..ctor(IEnumerable1 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

Anonymous
  • 11,748
  • 6
  • 35
  • 57
  • 2
    What does the stack trace look like? – Simon Whitehead May 15 '14 at 23:01
  • 1
    If this is your actual code, it seems like 'e' is sometimes null. It's definitely not 'q' being null. Do you add new items to the db.v_QCData that are null? – CodeCaster May 15 '14 at 23:02
  • no, new items are only inserted via stored procedures which always creates a dataID etc. –  May 15 '14 at 23:09
  • Do your procs return duplicate IDs? There seems to be an issue generating a hash/key for the items it returns.. Are you overriding `GetHashCode` in your entities? – Simon Whitehead May 15 '14 at 23:13
  • It could be that one of the members of your entity is marked as non-null, but there is a value in the database that is null. See http://stackoverflow.com/questions/10403390/why-am-i-getting-a-nullreferenceexception-in-system-data-entity-dll-entity-fram and http://stackoverflow.com/questions/4168136/entity-framework-nullreferenceexception-calling-tolist – hatchet - done with SOverflow May 15 '14 at 23:14
  • That's a good point, I'm going over the data model now to see if there is a nullable conflict. –  May 15 '14 at 23:34
  • If that's the case, post a comment to that effect, and this can be marked as a duplicate of one of the questions I linked. – hatchet - done with SOverflow May 15 '14 at 23:37

0 Answers0