1

对标题感到抱歉,但我真的想不出一种快速表达我想要什么的方式——如果你想到一个,有人可以把它改成更合适的吗?

我正在尝试将以下函数调用转换为表达式查询。

List.Compare("propretyName", ">1000");


public static IQueryable<T> Compare<T>(this IQueryable<T> source, string propertyName, string value) {
    Type type = typeof(T);
    ParameterExpression parameter = Expression.Parameter(type, "param");
    MemberExpression memberAccess = Expression.MakeMemberAccess(parameter, type.GetProperty(propertyName));

    string comparisonType = value.Substring(0, 1);
    value = value.Length > 0 ? value.Substring(1) : "0";

    decimal tmpvalue;
    decimal? result = decimal.TryParse(value, out tmpvalue) ? tmpvalue : (decimal?)null;
    ConstantExpression constant = Expression.Constant(result, typeof(decimal?));
    BinaryExpression comparisonExpression = Expression.GreaterThan(memberAccess, constant);
    switch (comparisonType) {
        case ">":
            comparisonExpression = Expression.GreaterThan(memberAccess, constant);
            break;
        case "<":
            comparisonExpression = Expression.LessThan(memberAccess, constant);
            break;
        case "=":
            comparisonExpression = Expression.Equal(memberAccess, constant);
            break;
    }

    Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(comparisonExpression, parameter);
    return source.Where(lambda);
}

以上是我为进行该调用而编写的方法。

底部的 lambda 似乎是正确的:{param => (param.propretyName > 1000)}

但是,它不起作用,我认为这是因为我正在研究的特定属性是decimal?应该的{param => (param.propertyName.Value > 1000)}

任何人都可以帮助我使用价值而不是。这里有一些东西在逃避我。

我无法使用该Where(string)方法,因为我正在使用实体框架。

找到答案

public static IQueryable<T> Compare<T>(this IQueryable<T> source, string propertyName, string value) {
    Type type = typeof(T);
    ParameterExpression parameter = Expression.Parameter(type, "param");
    MemberExpression memberAccess = Expression.MakeMemberAccess(parameter, type.GetProperty(propertyName));

    //This is the added methods that results in the proper output
    PropertyInfo valProp = typeof(Nullable<decimal>).GetProperty("Value");
    memberAccess = Expression.MakeMemberAccess(memberAccess, valProp);

    string comparisonType = value.Substring(0, 1);
    value = value.Length > 0 ? value.Substring(1) : "0";

    decimal tmpvalue;
    decimal? result = decimal.TryParse(value, out tmpvalue) ? tmpvalue : (decimal?)null;
    ConstantExpression constant = Expression.Constant(tmpvalue);
    BinaryExpression comparisonExpression = Expression.GreaterThan(memberAccess, constant);
    switch (comparisonType) {
        case ">":
            comparisonExpression = Expression.GreaterThan(memberAccess, constant);
            break;
        case "<":
            comparisonExpression = Expression.LessThan(memberAccess, constant);
            break;
        case "=":
            comparisonExpression = Expression.Equal(memberAccess, constant);
            break;
    }

    Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(comparisonExpression, parameter);
    return source.Where(lambda);
}
4

1 回答 1

1

Why not just use the existing Dynamic LINQ library:

myList.Where("propertyName > 1000");
于 2011-02-22T23:16:26.917 回答