用短日期构建动态表达式

时间:2014-12-17 11:18:25

标签: c#

我有以下代码可以帮助我构建动态表达式。但是,当我尝试比较Date时,它会将我的值转换为完整的DateTime标记。如何才能构建我的表达式,以便只比较短日期?

private static Expression GetExpression<T>(ParameterExpression param,
    Filter filter)
{
    MemberExpression member = Expression.Property(param, filter.PropertyName);
    PropertyInfo[] props = typeof (T).GetProperties();

    foreach (var property in props)
    {
        if (filter.Value != null)
        {
            if (property.Name == filter.PropertyName)
            {
                ConstantExpression constant = Expression.Constant(
                    Convert.ChangeType(filter.Value,
                        property.PropertyType));

                if (property.PropertyType == typeof (System.String))
                {
                    constant = Expression.Constant(
                        Convert.ChangeType(filter.Value.ToString().ToLower(),
                            property.PropertyType));
                }
                else if (property.PropertyType == typeof (System.DateTime))
                {


                    constant =
                        Expression.Constant(Convert.ChangeType(filter.Value, property.PropertyType));

                }
            }

        }
    }

    return null;
}

我知道将Convert.ChangeType转换为DateTime,但我得到的是item =&gt; item.ClosedOn == 11/11/2014 12:00:00 AM,当我想要item =&gt; item.ClosedOn == 11/11/2014

1 个答案:

答案 0 :(得分:0)

最后一行的变化:

filter.Value 

datestring