Get all 'where' calls using ExpressionVisitor

前端 未结 1 1921
说谎
说谎 2020-12-17 05:12

I have a query, like such:

var query = from sessions in dataSet
                    where (names.Contains(sessions.Username))
                    where (sess         


        
相关标签:
1条回答
  • 2020-12-17 06:07

    Use the class found here http://msdn.microsoft.com/en-us/library/bb882521%28v=vs.90%29.aspx as your base. You can then create your Visitor like this

    internal class WhereFinder : ExpressionVisitor
        {
            private IList<MethodCallExpression> whereExpressions = new List<MethodCallExpression>();
    
            public IList<MethodCallExpression> GetWhere(Expression expression)
            {
                Visit(expression); 
                return whereExpressions;
            }
    
            protected override Expression VisitMethodCall(MethodCallExpression expression)
            {
                if (expression.Method.Name == "Where")
                    whereExpressions.Add(expression);
    
                Visit(expression.Arguments[0]);
    
                return expression;
            }
        }
    
    0 讨论(0)
提交回复
热议问题