- 优化 表达式中不能使用 c# 函数的问题,

> 如:where(a => HttpContext.Session.GetString("UserID") == a.UserId)
This commit is contained in:
28810
2019-07-29 10:27:39 +08:00
parent 4609c910dd
commit 256963907e
3 changed files with 27 additions and 2 deletions

View File

@ -86,6 +86,13 @@ namespace System.Linq.Expressions
var body = Expression.Not(exp.Body);
return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
}
internal static bool IsParameter(this Expression exp)
{
var test = new TextParameterExpressionVisitor();
test.Visit(exp);
return test.Result;
}
}
internal class NewExpressionVisitor : ExpressionVisitor
@ -102,4 +109,15 @@ namespace System.Linq.Expressions
protected override Expression VisitParameter(ParameterExpression node) =>
node == _oldParameter ? this._newParameter : node;
}
internal class TextParameterExpressionVisitor : ExpressionVisitor
{
public bool Result { get; private set; }
protected override Expression VisitParameter(ParameterExpression node)
{
if (!Result) Result = true;
return node;
}
}
}