Closed lynnmiller closed 6 years ago
Hello @lynnmiller ,
The problem is caused because the dateTimeA
variable is a DateTime value, not a nullable DateTime when assigned.
DateTime? dateTimeA;
dateTimeA = new DateTime(2017, 1, 1);
var type = dateTimeA.GetType(); // = DateTime ... not DateTime?
Obviously, HasValue
doesn't exists on a DateTime
which cause the error.
There is two way to solve this issue
When compiling, you can specify the type.
// COMPILE + EXECUTE
DateTime? dateTimeA;
var compiled = Eval.Compile<Func<DateTime?, int>>("dateTimeA.HasValue ? 1 : 0", "dateTimeA");
dateTimeA = new DateTime(2017, 1, 1);
int r5 = compiled(dateTimeA);
dateTimeA = null;
int r6 = compiled(dateTimeA);
Don't use a nullable variable anymore, add the value in a field or property in a class instead. In this way, the true type will be able to be found.
public class EntityWithNullable
{
public DateTime? dateTimeA;
}
var entityWithNullable = new EntityWithNullable() {dateTimeA = new DateTime(2017, 1, 1)};
int r3 = Eval.Execute<int>("dateTimeA.HasValue ? 1 : 0", entityWithNullable);
entityWithNullable.dateTimeA = null;
int r4 = Eval.Execute<int>("dateTimeA.HasValue ? 1 : 0", entityWithNullable);
Let me know if that help you to solve your issue.
Best Regards,
Jonathan
Hello @lynnmiller ,
I will close this issue but if you have more question, feel free to reopen it.
Best Regards,
Jonathan
My comments and questions are in the code below: