在linq到nhibernate查询的Count中的null引用异常

时间:2012-06-30 05:15:28

标签: c# linq nhibernate lambda linq-to-nhibernate

我有NHibernate的linq查询。

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

Count中的检查条件无效。因为如果MyPerson的值为null,则计数的预期结果为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)

2 个答案:

答案 0 :(得分:1)

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);

答案 1 :(得分:0)

每当你在任何代码行中都有NullReferenceException并且不清楚它可以从哪里生成时,就开始将你的长行代码拆分成较小的代码,并进行空检查。

例如:

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");
}