Exemplo n.º 1
0
    public T Execute <T>(Expression expression)
    {
        //解析表达式,这里的T可能是泛型本身,也可能是集合,或者是动态值
        MethodCallExpression         methodCall = expression as MethodCallExpression;
        Expression <Func <T, bool> > result     = null;

        while (methodCall != null)
        {
            Expression       method = methodCall.Arguments[0];
            Expression       lambda = methodCall.Arguments[1];
            LambdaExpression right  = (lambda as UnaryExpression).Operand as LambdaExpression;
            if (result == null)
            {
                result = Expression.Lambda <Func <T, bool> >(right.Body, right.Parameters);
            }
            else
            {
                Expression left = result.Body;
                Expression temp = Expression.And(right.Body, left);
                result = Expression.Lambda <Func <T, bool> >(temp, result.Parameters);
            }
            methodCall = method as MethodCallExpression;
        }

        var cmdText = string.Format("SELECT * FROM {0}", typeof(T).Name);

        if (result != null)
        {
            var resolve = new ResolveExpression(_repository.DataSource);
            resolve.ResolveToSql(result);
            cmdText = string.Format("{0} WHERE {1}", cmdText, resolve.SqlWhere);
            //Command.Parameters.AddRange(resolve.Paras);
            //_repository.GetEntity<T>(cmdText,resolve.Paras);
        }


        //_resolveExpression.ResolveToSql(result);
        //var source = new DBSql().FindAs<T>(result);
        //var t = _repository.GetEntity<T>(result);
        return(default);