我有以下代码可以帮助我通过反射构建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
。
答案 0 :(得分:3)
您希望传递Convert.ChangeType(...)
第三个参数,即已存在的IFormatProvider
,用于此目的:DateTimeFormatInfo
。