Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a PartialEvaluationExceptionExpression and how do I fix it?

I have some code with LINQ query using nHibernate and when it executes, it throws a PartialEvaluationExceptionExpression. 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 Model is:

public class Filter
{
   DateTime? date;
}

Exception is caused by the false path of the ternary operator:

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

Even if I modify it to:

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

it still throws the exception.

like image 416
Arkadiusz Kałkus Avatar asked Feb 06 '15 21:02

Arkadiusz Kałkus


1 Answers

You're running code that's getting the value of a null object, and so its throwing. When the query provider tries to translate that query into something that the database can execute it needs to resolve Model.date.Value.Date into its value, so that the value can be used in the query. Since there is no value, the code breaks.

The solution of course is to not inline a check like this into the query itself. Determine, outside of the context of the query, whether or not you should be adding this check, and then only add it if needed:

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

Here the query provider is only ever given a value to evaluate when there actually is a value to be evaluated.

like image 75
Servy Avatar answered Nov 16 '22 17:11

Servy