How to implement LessThan, etc., when building expressions on strings

前端 未结 1 663
无人及你
无人及你 2021-01-05 06:30

I have a package in which I\'m building expression trees, to use with EntityFramework, via PredicateBuilder:

public Expression> con         


        
相关标签:
1条回答
  • 2021-01-05 06:58

    This works:

    Expression comparison = null;
    
    if (value.Type == typeof (string))
    {
        if (operation == ExpressionType.GreaterThanOrEqual ||
            operation == ExpressionType.GreaterThan ||
            operation == ExpressionType.LessThanOrEqual ||
            operation == ExpressionType.LessThan)
        {
            var method = value.Type.GetMethod("CompareTo", new[] {typeof (string)});
            var zero = Expression.Constant(0);
    
            var result = Expression.Call(member, method, converted);
    
            comparison = Expression.MakeBinary(operation, result, zero);
        }
    }
    
    if (comparison == null)
        comparison = Expression.MakeBinary(operation, member, converted);
    
    var lambda = Expression.Lambda<Func<T, bool>>(comparison, parameter);
    
    0 讨论(0)
提交回复
热议问题