2

次のように表現すると仮定します。

someIQueryable.Where(x => x.SomeBoolProperty)
someIQueryable.Where(x => !x.SomeBoolProperty)

上記のような式を次のような式に変換 (式ビジターを使用して書き換え) する必要があります。

someIQueryable.Where(x => x.SomeBoolProperty == true)
someIQueryable.Where(x => x.SomeBoolProperty != true)

注: より複雑な式がある場合、リライターはより一般的なケースでも機能する必要があります。

 someIQueryable.Where((x => x.SomeBoolProperty && x.SomeIntProperty > 0) || !x.SomeOtherBoolProperty))
4

1 に答える 1

7

何かのようなもの:

static class BooleanComplexifier
{
    public static Expression<T> Process<T>(Expression<T> expression)
        where T : class
    {
        var body = expression.Body;
        if (body.Type == typeof(bool))
        {
            switch(body.NodeType)
            {
                case ExpressionType.Equal:
                case ExpressionType.NotEqual:
                case ExpressionType.GreaterThan:
                case ExpressionType.GreaterThanOrEqual:
                case ExpressionType.LessThan:
                case ExpressionType.LessThanOrEqual:
                    return expression;
                case ExpressionType.Not:
                    body = Expression.NotEqual(
                        ((UnaryExpression)body).Operand,
                        Expression.Constant(true));
                    break;
                default:
                        body = Expression.Equal(body,
                        Expression.Constant(true));
                    break;
            }
            return Expression.Lambda<T>(body, expression.Parameters);
        }
        return expression;
    }   
}

と:

Expression<Func<Foo, bool>> x = foo => foo.IsAlive,
    y = foo => !foo.IsAlive;

var a = BooleanComplexifier.Process(x); // foo => foo.IsAlive == true
var b = BooleanComplexifier.Process(y); // foo => foo.IsAlive != true
//...
class Foo
{
    public bool IsAlive { get;set; }
}

より複雑な処理では、次のExpressionVisitorものが必要になる場合があります。

class BooleanComplexifier : ExpressionVisitor
{
    public static Expression<T> Process<T>(Expression<T> expression)
    {
        return (Expression<T>)new BooleanComplexifier().Visit(expression);
    }

    int bypass;
    protected override Expression VisitBinary(BinaryExpression node)
    {
        if (bypass == 0 && node.Type == typeof(bool))
        {
            switch (node.NodeType)
            {
                case ExpressionType.And: // bitwise & - different to &&
                case ExpressionType.Or: // bitwise | - different to ||
                case ExpressionType.Equal:
                case ExpressionType.NotEqual:
                    bypass++;
                    var result = base.VisitBinary(node);
                    bypass--;
                    return result;
            }
        }
        return base.VisitBinary(node);
    }
    protected override Expression VisitUnary(UnaryExpression node)
    {
        if (bypass == 0 && node.Type == typeof(bool))
        {
            switch(node.NodeType)
            {
                case ExpressionType.Not:
                    bypass++;
                    var result = Expression.NotEqual(
                        base.Visit(node.Operand),
                        Expression.Constant(true));
                    bypass--;
                    return result;
            }
        }
        return base.VisitUnary(node);
    }
    protected override Expression VisitMember(MemberExpression node)
    {
        if(bypass == 0 && node.Type == typeof(bool))
        {
            return Expression.Equal(
                base.VisitMember(node),
                Expression.Constant(true));
        }
        return base.VisitMember(node);
    }
}
于 2014-01-27T13:39:59.903 に答える