簡體   English   中英

linq到nhibernate查詢的計數中的空引用異常

[英]null reference exception in Count of linq to nhibernate query

我有一個NHibernate的linq查詢。

var q = SessionInstance.Query<Identity>()
       .Count(x => MyPerson != null && x.Person.Id == MyPerson.Id);

但是“ Count ”中的“檢查條件”無效。 因為如果MyPerson值為null,則count的預期結果為0但是此查詢具有NullReferenceException通過此消息:

Object reference not set to an instance of an object.

為什么?

堆棧跟蹤為:

   at lambda_method(Closure )
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.EvaluateSubtree(Expression subtree)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitLambdaExpression(LambdaExpression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitUnaryExpression(UnaryExpression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](T expression, String methodName)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.<>c__DisplayClass6`1.<VisitAndConvert>b__5(T expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitList[T](ReadOnlyCollection`1 list, Func`2 visitMethod)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](ReadOnlyCollection`1 expressions, String callerName)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression)
   at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.EvaluateIndependentSubtrees(Expression expressionTree)
   at NHibernate.Linq.NhLinqExpression..ctor(Expression expression)
   at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source, Expression`1 predicate)
   at RCISP.NHibernate.Repository.IdentityRepositoryNh.IsExistOriginalIdentityForThisAlias(Identity item)
   at RCISP.Domain.Services.IdentityService.CreateIdentity(IRepositoryLocator locator, Person person)
   at RCISP.Domain.Services.IdentityService.LoadByPersonIdentificationCode(IRepositoryLocator locator, String identificationCode, Nullable`1 autoCreate)
   at RCISP.Domain.Services.EvidenceRequestService.Update[T](IRepositoryLocator locator, EvidenceRequestDto dto)
   at RCISP.Domain.Services.EvidenceRequestService.UpdateEntityCommand[T,TR](IRepositoryLocator locator, EvidenceRequestDto dto)
   at RCISP.Domain.Services.WeaponLicenseRequestService.<>c__DisplayClass4.<UpdateEntity>b__3(IRepositoryLocator locator)
   at RCISP.NHibernate.TransManager.TransManagerNh.ExecuteCommand[TResult](Func`2 command)
if(MyPerson != null)
{
  var q = SessionInstance.Query<Identity>()
       .Count(x => x.Person != null && x.Person.Id == MyPerson.Id);
}else
{
  // if you know when MyPerson is null count is zero, why you need to Query?
  // here count is zero..

}

編輯

  var q = SessionInstance.Query<Identity>()
       .Count(x => MyPerson != null && x.Person != null && x.Person.Id == MyPerson.Id);

每當在任何代碼行中都有NullReferenceException,並且不清楚它的起源時,就開始將您的長代碼拆分為較小的代碼,並執行null檢查。

例如:

var q = SessionInstance.Query<Identity>();

if (q != null && MyPerson != null) {
   var count = q.Count(x => 
       x != null &&
       x.Person != null &&
       x.Person.Id == MyPerson.Id
   );
}
else {
   throw new InvalidOperationException("Something went wrong");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM