I am trying to complete implementation of dynamic querying provided by Todd Sprang, please see this link. Currently I am trying to enable filtering by DateTime type, and I am struggling to make lambda expression using "greater than" and "less than" operators, so my SQL would appear like "WHERE someDate<='2017-04-27 00:00:00' AND someDate<='2017-04-27 23:59:59'.
I managed to extract an operator methods:
private static readonly MethodInfo DateTimeGreaterThanOrEqualMethod = typeof(DateTime).GetMethod("op_GreaterThanOrEqual",
BindingFlags.Static | BindingFlags.Public);
private static readonly MethodInfo DateTimeLessThanOrEqualMethod = typeof(DateTime).GetMethod("op_LessThanOrEqual",
BindingFlags.Static | BindingFlags.Public);
And here is my DateTime lambda filter method:
private static Expression<Func<TDbType, bool>> ApplyDateTimeCriterion<TDbType,
TSearchCriteria>(TSearchCriteria searchCriteria, PropertyInfo searchCriterionPropertyInfo,
Type dbType, MemberInfo dbFieldMemberInfo, Expression<Func<TDbType, bool>> predicate)
{
var searchDateTime = searchCriterionPropertyInfo.GetValue(searchCriteria) as DateTime?;
if (searchDateTime == null)
{
return predicate;
}
var valueDateMin = ((DateTime)searchDateTime).Date;
var valueDateMax = new DateTime(valueDateMin.Year, valueDateMin.Month, valueDateMin.Day, 23, 59, 59);
var dbTypeParameter = Expression.Parameter(dbType, @"x");
var dbFieldMember = Expression.MakeMemberAccess(dbTypeParameter, dbFieldMemberInfo);
var criterionConstantMin = new Expression[] { Expression.Constant(valueDateMin) };
var criterionConstantMax = new Expression[] { Expression.Constant(valueDateMax) };
//having problem down here, when trying to call static method which needs two parameters
var greaterThanCall = Expression.Call(dbFieldMember, DateTimeGreaterThanOrEqualMethod, criterionConstantMin);
var lambdaGreaterThan = Expression.Lambda(greaterThanCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;
var lessThanCall = Expression.Call(dbFieldMember, DateTimeLessThanOrEqualMethod, criterionConstantMax);
var lambdaLessThan = Expression.Lambda(greaterThanCall, dbTypeParameter) as Expression<Func<TDbType, bool>>;
return predicate.And(lambdaGreaterThan).And(lambdaLessThan);
}
I am having problem on calling Expression.Call because op_GreaterThanOrEqual is a static method with 2 parameters. Maybe I should not call op_GreaterThanOrEqual, maybe this has to be done in another way?