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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With