-->

建设有短日期动态拉姆达谓语(Building dynamic lambda predicate wi

2019-10-17 09:45发布

我有以下的代码,可以帮助我通过反射建立一个lambda表达式。 然而,当我尝试比较与一个Date是我的值转换为一个完整的DateTime戳记。 我怎样才能得到它来建立我的谓词所以它只会比较短日期?

System.Reflection.PropertyInfo propInfo = typeof(T).GetProperty(property);
Type propertyType = propInfo.PropertyType;
if (Utilities.IsNullableType(propertyType))
{
    propertyType = Nullable.GetUnderlyingType(propertyType);
}
ParameterExpression propAlias = Expression.Parameter(typeof(T), alias);
MemberExpression left = Expression.Property(propAlias, property);
ConstantExpression right = Expression.Constant(Convert.ChangeType(value, propertyType));
BinaryExpression comparer = BuildComparisonExpression(left, right, comparison);
return Expression.Lambda<Func<T, bool>>(comparer, propAlias);

我知道这是Convert.ChangeType是字符串转换为DateTime ,但我得到的回复是item => item.DateToCheck == 1/1/2012 12:00:00AM ,当我想item => item.DateToCheck == 1/1/2012

Answer 1:

你想通过Convert.ChangeType(...)第三个参数,已经存在IFormatProvider为此确切的目的: DateTimeFormatInfo



文章来源: Building dynamic lambda predicate with short date