コード例 #1
0
        public static Expression <Func <T, bool> > ConstructAndExpressionTree <T>(List <ExpressionFilter> filters)
        {
            if (filters.Count == 0)
            {
                return(null);
            }
            ParameterExpression param = Expression.Parameter(typeof(T), "t");
            Expression          exp   = null;

            if (filters.Count == 1)
            {
                exp = ExpressionRetriever.GetExpression <T>(param, filters[0]);
            }
            else
            {
                exp = ExpressionRetriever.GetExpression <T>(param, filters[0]);
                for (int i = 1; i < filters.Count; i++)
                {
                    exp = Expression.Or(exp, ExpressionRetriever.GetExpression <T>(param, filters[i]));
                }
            }
            return(Expression.Lambda <Func <T, bool> >(exp, param));
        }
コード例 #2
0
        public static Expression <Func <T, bool> > ConstructAndExpressionTree <T>(List <ExpressionFilter> filters, bool applyAndOpertor)
        {
            if (filters == null || filters.Count == 0)
            {
                return(null);
            }
            // create parameter
            ParameterExpression param = Expression.Parameter(typeof(T), "t");
            Expression          exp   = ExpressionRetriever.GetExpression <T>(param, filters[0], filters[0].PropertyType);

            for (int i = 1; i < filters.Count; i++)
            {
                if (applyAndOpertor)
                {
                    exp = Expression.And(exp, ExpressionRetriever.GetExpression <T>(param, filters[i], filters[i].PropertyType));
                }
                else
                {
                    exp = Expression.Or(exp, ExpressionRetriever.GetExpression <T>(param, filters[i], filters[i].PropertyType));
                }
            }
            return(Expression.Lambda <Func <T, bool> >(exp, param));
        }