Beispiel #1
0
        public void UpdatePartial <T>(Expression <Func <T> > columns)
        {
            Table tableOrCreate = TableCache.GetTableOrCreate(typeof(T));
            Dictionary <Column, object> tempDict = new Dictionary <Column, object>();
            MemberInitExpression        body     = columns.Body as MemberInitExpression;

            if (body == null)
            {
                throw new ApplicationException("部分更新只支持对象初始化的方式");
            }
            foreach (MemberBinding binding in body.Bindings)
            {
                if (binding.BindingType != MemberBindingType.Assignment)
                {
                    throw new ApplicationException("暂不支持复杂的属性初始化方式");
                }
                MemberAssignment   assignment  = binding as MemberAssignment;
                ConstantExpression expression2 = PartialEvaluator.Eval(assignment.Expression) as ConstantExpression;
                string             name        = assignment.Member.Name;
                if (expression2 == null)
                {
                    throw new ApplicationException(string.Format("无法计算属性{0}的值", name));
                }
                object obj2 = expression2.Value;
                Column columnByColumnName = tableOrCreate.GetColumnByColumnName(name);
                if (columnByColumnName == null)
                {
                    throw new ApplicationException(string.Format("在表{0}中找不到名为{1}的列", tableOrCreate.TableName, name));
                }
                tempDict[columnByColumnName] = obj2;
                Trace.WriteLine(string.Format("{0}-{1}", name, obj2));
            }
            Dictionary <Column, object> keyDict = tableOrCreate.KeyColumns.ToDictionary <Column, Column, object>(p => p, delegate(Column p) {
                if (!tempDict.ContainsKey(p))
                {
                    throw new ApplicationException(string.Format("未给主键字段{0}赋值", p.ColumnName));
                }
                return(tempDict[p]);
            });
            Dictionary <Column, object> valueDict = (from p in tempDict
                                                     where !p.Key.IsPrimaryKey
                                                     select p).ToDictionary <KeyValuePair <Column, object>, Column, object>(p => p.Key, p => p.Value);

            this.InternalUpdatePartial(typeof(T), keyDict, valueDict);
        }
Beispiel #2
0
        private LambdaExpression Parameterize(Expression query, out object[] arguments)
        {
            IQueryProvider provider = this.FindProvider(query);

            if (provider == null)
            {
                throw new ArgumentException("Cannot deduce query provider from query");
            }
            IEntityProvider            ep = provider as IEntityProvider;
            Func <Expression, bool>    fnCanBeEvaluated = (ep != null) ? new Func <Expression, bool>(ep.CanBeEvaluatedLocally) : null;
            List <ParameterExpression> parameters       = new List <ParameterExpression>();
            List <object> values     = new List <object>();
            Expression    expression = PartialEvaluator.Eval(query, fnCanBeEvaluated, delegate(ConstantExpression c) {
                bool flag = c.Value is IQueryable;
                if (!((flag || (ep == null)) || ep.CanBeParameter(c)))
                {
                    return(c);
                }
                ParameterExpression item = Expression.Parameter(c.Type, "p" + parameters.Count);
                parameters.Add(item);
                values.Add(c.Value);
                if (flag)
                {
                    return(c);
                }
                return(item);
            });

            if (expression.Type != typeof(object))
            {
                expression = Expression.Convert(expression, typeof(object));
            }
            arguments = values.ToArray();
            if (arguments.Length < 5)
            {
                return(Expression.Lambda(expression, parameters.ToArray()));
            }
            arguments = new object[] { arguments };
            return(ExplicitToObjectArray.Rewrite(expression, parameters));
        }
            internal IQueryProvider FindProvider(Expression expression, object[] args)
            {
                Func <object, int, ConstantExpression> selector = null;
                Expression expression2 = this.FindProviderInExpression(expression) as ConstantExpression;

                if (((expression2 == null) && (args != null)) && (args.Length > 0))
                {
                    if (selector == null)
                    {
                        selector = (a, i) => Expression.Constant(a, this.query.Parameters[i].Type);
                    }
                    Expression expression3 = ExpressionReplacer.ReplaceAll(expression, this.query.Parameters.ToArray <ParameterExpression>(), args.Select <object, ConstantExpression>(selector).ToArray <ConstantExpression>());
                    expression2 = this.FindProviderInExpression(expression3);
                }
                if (expression2 != null)
                {
                    ConstantExpression expression4 = expression2 as ConstantExpression;
                    if (expression4 == null)
                    {
                        expression4 = PartialEvaluator.Eval(expression2) as ConstantExpression;
                    }
                    if (expression4 != null)
                    {
                        IQueryProvider provider = expression4.Value as IQueryProvider;
                        if (provider == null)
                        {
                            IQueryable queryable = expression4.Value as IQueryable;
                            if (queryable != null)
                            {
                                provider = queryable.Provider;
                            }
                        }
                        return(provider);
                    }
                }
                return(null);
            }