I want to make a dynamic check for a null value. I want to make a where clause which will compare only the date part of the date field.
It will work for non nullable date fields, but for nullable date fields we need to check for value as using .Date on null data will throw an error
let us say
p => (p.Date.Value == null ? null : p.Date.Value.Date) == SelectedDate.Date
or
p => ( p.Date.Value == null ? p.Date.Value : p.Date.Value.Date) == SelectedDate.Date
or
p => (p.Date.Value == null ? p.Date : p.Date.Value.Date) == SelectedDate.Date
basically a null checking ternary operator which selects only the date part of
I already tried
ConstantExpression argument = Expression.Constant(MyDateField, typeof(DateTime));
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
string field = "Date";
BinaryExpression condition = Expression.Equal(Expression.Property(parameter, field), Expression.Constant(null, typeof(DateTime?)));
ConditionalExpression ternary = Expression.Condition(condition, property, Expression.Property(property, "Date"));
Expression equalExp = Expression.Equal(ternary, argument);
lambda = Expression.Lambda<Func<T, bool>>(equalExp, parameter);
Which gives me
p => (IIF((p.EventDate == null), p.EventDate.Value, p.EventDate.Value.Date) == 21-Jun-18 12:00:00 AM)
but this is not working. Issue I'm facing is
If I use p.Date.Value in the BinaryExpression then it doesnot allow as .Value makes it DateTime and null is only available in DateTime?
IIF
condition is generated and not ?:
ternary operator
Any and all help is appreciated.