What is PartialEvaluationExceptionExpression and How to Fix It?

I have a LINQ query code using nHibernate, and when it is executed, it throws a PartialEvaluationExceptionExpression expression. What exactly does this mean, and what can I do about it?

SomeIqueryableNhibernateObject .Where(x=>... some expression && !Model.date.HasValue ? true : (x.fooDate.Date == Model.date.Value.Date) && some expresion 

Where is the model:

 public class Filter { DateTime? date; } 

The exception is caused by the false path of the ternary operator:

 x.fooDate.Date == Model.date.Value.Date 

Even if I change it to:

 x.fooDate != null && Model.date.HasValue && x.fooDate.Date == Model.date.Value.Date 

it still throws an exception.

+7
c # linq nhibernate
source share
1 answer

You run code that gets the value of the null object and therefore its throwing. When a query provider tries to translate this query into something that the database can execute, it needs to allow Model.date.Value.Date to its value so that this value can be used in the query. Since there is no value, the code is aborted.

Of course, the solution is to not include such a check in the request itself. Determine, outside the context of the request, whether to add this check or not, and then add it only if necessary:

 var query = CreateInitialQuery(); if(Model.date.HasValue) query = query.Where(x => x.fooDate.Date == Model.date.Value.Date); 

Here, the query provider is always assigned a value for evaluation when there is actually a value for evaluation.

+7
source share

All Articles