예제 #1
0
        private static object GetValueOfMethadCallExpression(MethodCallExpression expression, SqlPack sqlPack)
        {
            object result = "";

            if (expression.Object != null)
            {
                if (expression.Object is MemberExpression)
                {
                    var memberExp = expression.Object as MemberExpression;
                    result = GetValueOfMemberExpression(memberExp, sqlPack);
                }
                else if (expression.Object is ConstantExpression)
                {
                    var constantExp = expression.Object as ConstantExpression;
                    if (constantExp != null)
                    {
                        result = (constantExp.GetValueOfExpression(sqlPack));
                    }
                }
                else if (expression.Object is MethodCallExpression)
                {
                    var constantExp = expression.Object as MethodCallExpression;
                    if (constantExp != null)
                    {
                        result = MethodCallFluentExpressionSQL.GetMethodResult(constantExp, sqlPack, true);
                    }
                }
            }
            return(result);
        }
예제 #2
0
        //public static object GetValueOfExpressionWithoutFormat(this Expression body )
        //{
        //    if (body == null)
        //    {
        //        return string.Empty;
        //    }
        //    if (body is ConstantExpression)
        //    {
        //        return ((ConstantExpression)body).Value;
        //    }
        //    if (body is MemberExpression)
        //    {
        //        var member = ((MemberExpression)body);

        //        var value = GetValueOfMemberExpression(member);
        //        Type typeOfValue = null;
        //        if (value != null)
        //        {
        //            typeOfValue = value.GetType();
        //        }

        //        return (value);

        //    }
        //    if (body is UnaryExpression)
        //    {
        //        return GetValueOfExpressionWithoutFormat(((UnaryExpression)body).Operand);
        //    }
        //    if (body is NewExpression)
        //    {
        //        var args = ((NewExpression)body).Arguments;
        //        //判断Select设置值  Select<UserInfo>(u => new {  DT3=new DateTime(2015,10,11) }).
        //        if (args.Count > 0)
        //        {
        //            return GetValueOfExpressionWithoutFormat(args[0]);
        //        }
        //        //foreach (var item in ((NewExpression)body).Arguments)
        //        //{
        //        //    GetValueOfExpression(item, sqlpack);
        //        //}
        //    }
        //    //if (body is BinaryExpression)
        //    //{
        //    //    var binary = body as BinaryExpression;
        //    //    if (binary.Right.GetValueOfExpressionWithoutFormat() == null && (binary.NodeType == ExpressionType.Equal || binary.NodeType == ExpressionType.NotEqual))
        //    //    {
        //    //        return string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack),
        //    //        ConvertExpressionTypeToString(binary.NodeType, true),
        //    //        "NULL");
        //    //    }
        //    //    else
        //    //    {
        //    //        return string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack),
        //    //       ConvertExpressionTypeToString(binary.NodeType),
        //    //       GetValueOfExpression(binary.Right, sqlpack));
        //    //    }

        //    //}
        //    //if (body is MethodCallExpression)
        //    //{
        //    //    var method = body as MethodCallExpression;

        //    //    return MethodCallFluentExpressionSQL.GetMethodResult(method, sqlpack);

        //    //}
        //    if (body is LambdaExpression)
        //    {
        //        return GetValueOfExpressionWithoutFormat(((LambdaExpression)body).Body);
        //    }
        //    return "";
        //}

        public static object GetValueOfExpression(this Expression body, SqlPack sqlpack, bool needFormat = true)
        {
            if (body == null)
            {
                return(string.Empty);
            }
            if (body is ConstantExpression)
            {
                return(FormatValue(((ConstantExpression)body).Value, sqlpack, needFormat));
            }
            if (body is MemberExpression)
            {
                var member = ((MemberExpression)body);


                if (member.Member.MemberType == MemberTypes.Property)
                {
                    if (member.Member.DeclaringType == ResolveConstants.TypeOfDateTime)//日期转换
                    {
                        if (member.Member == ResolveConstants.PropertyInfo_DateTime_Now ||
                            member.Member == ResolveConstants.PropertyInfo_DateTime_UtcNow ||
                            member.Member == ResolveConstants.PropertyInfo_DateTime_Today)//只能转换为常量的SQL 字符
                        {
                            object valueDatetime = sqlpack.SqlDialectProvider.ConvertDateTime(member.Member, null);
                            return(valueDatetime);
                        }
                    }



                    string tableName = GetTableNameByExpression(member, sqlpack);
                    sqlpack.SetTableAlias(tableName);
                    string tableAlias = sqlpack.GetTableAlias(tableName);
                    if (!string.IsNullOrWhiteSpace(tableAlias))
                    {
                        tableAlias += ".";
                    }
                    return(tableAlias + member.Member.Name);

                    //return member.Member.Name;
                }
                var  value       = GetValueOfMemberExpression(member);
                Type typeOfValue = null;
                if (value != null)
                {
                    typeOfValue = value.GetType();
                }

                if (IsListType(typeOfValue))
                {
                    var valueList = value as IEnumerable;

                    var sb = new StringBuilder();
                    foreach (var item in value as IEnumerable)
                    {
                        sb.AppendFormat("{0},", FormatValue(item, sqlpack, needFormat));
                    }
                    if (sb.Length == 0)
                    {
                        return("");
                    }
                    return(sb.Remove(sb.Length - 1, 1).ToString());
                }
                else
                {
                    return(FormatValue(value, sqlpack, needFormat));
                }
            }
            if (body is UnaryExpression)
            {
                return(GetValueOfExpression(((UnaryExpression)body).Operand, sqlpack, needFormat));
            }
            if (body is NewExpression)
            {
                var args = ((NewExpression)body).Arguments;
                //判断Select设置值  Select<UserInfo>(u => new {  DT3=new DateTime(2015,10,11) }).
                if (args.Count > 0)
                {
                    return(GetValueOfExpression(args[0], sqlpack, needFormat));
                }
                //foreach (var item in ((NewExpression)body).Arguments)
                //{
                //    GetValueOfExpression(item, sqlpack);
                //}
            }
            if (body is BinaryExpression)
            {
                var binary = body as BinaryExpression;
                if (binary.Right.GetValueOfExpression(sqlpack, needFormat) == null && (binary.NodeType == ExpressionType.Equal || binary.NodeType == ExpressionType.NotEqual))
                {
                    return(string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack, needFormat),
                                         ConvertExpressionTypeToString(binary.NodeType, true),
                                         "NULL"));
                }
                else
                {
                    return(string.Format("({0}{1}{2})", GetValueOfExpression(binary.Left, sqlpack, needFormat),
                                         ConvertExpressionTypeToString(binary.NodeType),
                                         GetValueOfExpression(binary.Right, sqlpack, needFormat)));
                }
            }
            if (body is MethodCallExpression)
            {
                var method = body as MethodCallExpression;

                return(MethodCallFluentExpressionSQL.GetMethodResult(method, sqlpack, needFormat));

                //return string.Format("({0} IN ({1}))", GetValueOfExpression(method.Arguments[0], sqlpack),
                //    GetValueOfExpression(method.Object, sqlpack));
            }
            if (body is LambdaExpression)
            {
                return(GetValueOfExpression(((LambdaExpression)body).Body, sqlpack, needFormat));
            }
            return("");
        }