gpt4 book ai didi

c# - 如何将表达式 x=>!x 重写为 x=>x!=true 并将 x=>x 重写为 x=>x==true

转载 作者:行者123 更新时间:2023-11-30 20:00:18 30 4
gpt4 key购买 nike

假设,我们这样表达:

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))

最佳答案

类似于:

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);
}
}

关于c# - 如何将表达式 x=>!x 重写为 x=>x!=true 并将 x=>x 重写为 x=>x==true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382175/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com