コード例 #1
0
        public static Dictionary <string, string> BuildColumns(Expression expression, Dictionary <string, object> param, bool singleTable = true)
        {
            var columns = new Dictionary <string, string>();

            if (expression is LambdaExpression)
            {
                expression = (expression as LambdaExpression).Body;
            }
            if (expression is MemberInitExpression)
            {
                var initExpression = (expression as MemberInitExpression);
                for (int i = 0; i < initExpression.Bindings.Count; i++)
                {
                    var        column   = string.Empty;
                    Expression argument = (initExpression.Bindings[i] as MemberAssignment).Expression;
                    if (argument is UnaryExpression)
                    {
                        argument = (argument as UnaryExpression).Operand;
                    }
                    if (argument is MethodCallExpression && (argument as MethodCallExpression).Method.DeclaringType == typeof(Convert))
                    {
                        column = GetValue((argument as MethodCallExpression).Arguments[0]).ToString();
                    }
                    else if (argument is ConstantExpression || (argument is MemberExpression && (argument as MemberExpression).Expression is ConstantExpression))
                    {
                        column = GetValue(argument).ToString();
                    }
                    else
                    {
                        column = BuildExpression(argument, param, singleTable);
                    }
                    var name = initExpression.Bindings[i].Member.Name;
                    columns.Add(name, column);
                }
            }
            else if (expression is NewExpression)
            {
                var newExpression = (expression as NewExpression);
                for (int i = 0; i < newExpression.Arguments.Count; i++)
                {
                    var columnName = string.Empty;
                    var argument   = newExpression.Arguments[i];
                    if (argument is MethodCallExpression && (argument as MethodCallExpression).Method.DeclaringType == typeof(Convert))
                    {
                        columnName = GetValue((argument as MethodCallExpression).Arguments[0]).ToString();
                    }
                    else if (argument is ConstantExpression || (argument is MemberExpression && (argument as MemberExpression).Expression is ConstantExpression))
                    {
                        columnName = GetValue(argument).ToString();
                    }
                    else
                    {
                        columnName = BuildExpression(argument, param, singleTable);
                    }
                    var name = newExpression.Members[i].Name;
                    columns.Add(name, columnName);
                }
            }
            else if (expression is MemberExpression)
            {
                var memberExpression = (expression as MemberExpression);
                var name             = memberExpression.Member.Name;
                var columnName       = EntityUtil.GetColumn(memberExpression.Expression.Type, f => f.CSharpName == name)?.ColumnName ?? name;
                if (!singleTable)
                {
                    var tableName = EntityUtil.GetTable(memberExpression.Expression.Type).TableName;
                    columnName = string.Format("{0}.{1}", tableName, columnName);
                }
                columns.Add(name, columnName);
            }
            else
            {
                var name       = string.Format("COLUMN0");
                var columnName = BuildExpression(expression, param, singleTable);
                columns.Add(name, columnName);
            }
            return(columns);
        }