linq中的“对象引用未设置为对象的实例”

时间:2014-05-15 22:56:46

标签: c# sql linq data-modeling

我是Linq的新手,不得不接管一个前同事项目。我一直在寻找,但我仍然对这个问题感到有点难过......

我有以下代码:

var q = db.v_QCData.OrderByDescending(e => e.DataID).Take(MAX_WAFER_LIST);

QcWaferList = null;
if (q != null)
{
    QcWaferList = q.ToList();

db是指数据模型。这在某些时候很有用,所以我知道模型很好,但在ToList()的其他时候,我得到的Object引用没有设置为对象的实例。我无法理解为什么它应该在某些时候是空的而不是其他的。所以我尝试了这个:

bool has = db.v_QCData.Any();

返回true,但我无法理解的是它如何在其中包含数据,但不返回任何数据。任何建议都表示赞赏。

编辑: 第162行是q.ToList();

  

堆栈跟踪:在System.Data.EntityKey.AddHashValue(Int32 hashCode,   对象keyValue)\ r \ n在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的   密钥)\ r \ n在System.Collections.Generic.Dictionary 2.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   在lambda_method(Closure,Shaper)\ r \ n at   System.Data.Common.Internal.Materialization.Coordinator 1.ReadNextElement(Shaper shaper)\r\n at System.Data.Common.Internal.Materialization.Shaper 1.SimpleEnumerator.MoveNext(个)\ r \ n   在System.Collections.Generic.List 1..ctor(IEnumerable 1   收藏)\ r \ n at   System.Linq.Enumerable.ToList [TSource](IEnumerable`1 source)\ r \ n at   Solar1366Tech.LaserQCHMI.ViewModels.MachineViewModel.UpdateWaferList()   在c:\ TFS2012 \ Direct   晶圆\ DWWF03 \ Solar1366Tech.LaserQCHMI \ Solar1366Tech.LaserQCHMI \的ViewModels \ MachineViewModel.cs:行   162

0 个答案:

没有答案