/// <summary>
        /// 按匿名表达式更新
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="expression"></param>
        /// <param name="newExpress"></param>
        /// <returns></returns>
        public int Update <TResult>(Expression <Func <T, bool> > expression, Expression <Func <T, TResult> > newExpress)
        {
            var c = new ParameCollection();

            if (newExpress.Body is NewExpression)
            {
                var newExp = newExpress.Body as NewExpression;
                for (int i = 0; i < newExp.Members.Count; i++)
                {
                    var  m = newExp.Members[i];
                    var  v = newExp.Arguments[i];
                    bool cos;
                    var  value = ConstantValueVisitor.GetMemberExpressionValue(v, out cos);
                    c.Add(m.Name, value);
                }
            }
            else if (newExpress.Body is MemberInitExpression)
            {
                var memberInitExp = (newExpress.Body as MemberInitExpression);

                foreach (MemberAssignment m in memberInitExp.Bindings)
                {
                    bool cos;
                    var  value = ConstantValueVisitor.GetMemberExpressionValue(m.Expression, out cos);
                    c.Add(m.Member.Name, value);
                }
            }
            var db = DBExtend;
            var n  = db.Update(expression, c);

            return(n);
        }
Beispiel #2
0
        internal ParameCollection GetUpdateField <TModel>(TModel obj) where TModel : class
        {
            ParameCollection c = new ParameCollection();

            if (obj is IModel)
            {
                c = (obj as IModel).GetUpdateField(_DBAdapter);//手动指定变更
                if (c.Count > 0)
                {
                    return(c);
                }
            }

            var fields = TypeCache.GetProperties(typeof(TModel), true);//全量更新

            foreach (var f in fields.Values)
            {
                if (f.IsPrimaryKey)
                {
                    continue;
                }
                c.Add(f.MemberName, f.GetValue(obj));
            }
            return(c);
        }
Beispiel #3
0
        /// <summary>
        /// 获取被修改的字段
        /// </summary>
        /// <returns></returns>
        public ParameCollection GetUpdateField(bool check = true)
        {
            var c      = new ParameCollection();
            var fields = TypeCache.GetProperties(GetType(), true);

            if (this.GetChanges().Count > 0)//按手动指定更改
            {
                foreach (var item in this.GetChanges())
                {
                    var key = item.Key.Replace("$", "");
                    var f   = fields[key];
                    if (f == null)
                    {
                        continue;
                    }
                    if (f.IsPrimaryKey || f.FieldType != Attribute.FieldType.数据库字段)
                    {
                        continue;
                    }
                    var value = item.Value;
                    //如果表示值为被追加 名称为$name
                    //使用Cumulation扩展方法后按此处理
                    if (key != item.Key)//按$name=name+'123123'
                    {
                        if (f.PropertyType == typeof(string))
                        {
                            value = string.Format("{0}+'{1}'", key, value);
                        }
                        else
                        {
                            value = string.Format("{0}+{1}", key, value);
                        }
                    }
                    c[item.Key] = value;
                }
                return(c);
            }
            //按对象对比
            var origin = this.OriginClone;

            if (origin == null && check)
            {
                throw new CRLException("_originClone为空,请确认此对象是由查询创建");
            }
            foreach (var f in fields.Values)
            {
                if (f.IsPrimaryKey)
                {
                    continue;
                }
                var originValue  = f.GetValue(origin);
                var currentValue = f.GetValue(this);
                if (!Object.Equals(originValue, currentValue))
                {
                    c.Add(f.MemberName, currentValue);
                }
            }
            return(c);
        }
Beispiel #4
0
        /// <summary>
        /// 获取被修改的字段
        /// </summary>
        /// <returns></returns>
        public ParameCollection GetUpdateField(DBAdapter.DBAdapterBase dBAdapterBase = null, bool check = true)
        {
            var c      = new ParameCollection();
            var fields = TypeCache.GetProperties(GetType(), true);

            if (this.GetChanges().Count > 0)//按手动指定更改
            {
                foreach (var item in this.GetChanges())
                {
                    var key = item.Key.Replace("$", "");
                    var f   = fields[key];
                    if (f == null)
                    {
                        continue;
                    }
                    if (f.IsPrimaryKey)
                    {
                        continue;
                    }
                    var value = item.Value;
                    //如果表示值为被追加 名称为$name
                    //使用Cumulation扩展方法后按此处理
                    if (key != item.Key)//按$name=name+'123123'
                    {
                        if (dBAdapterBase != null)
                        {
                            value = dBAdapterBase.GetFieldConcat(dBAdapterBase.KeyWordFormat(f.MapingName), value, f.PropertyType);
                        }
                    }
                    c[item.Key] = value;
                }
                return(c);
            }
            //按对象对比
            var origin = this.OriginClone;

            if (origin == null && check)
            {
                throw new CRLException("_originClone为空,请确认此对象是由查询创建");
            }
            foreach (var f in fields.Values)
            {
                if (f.IsPrimaryKey)
                {
                    continue;
                }
                var originValue  = f.GetValue(origin);
                var currentValue = f.GetValue(this);
                if (!Equals(originValue, currentValue))
                {
                    c.Add(f.MapingName, currentValue);
                }
            }
            return(c);
        }
Beispiel #5
0
        /// <summary>
        /// 按匿名对象更新
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="expression"></param>
        /// <param name="updateValue"></param>
        /// <returns></returns>
        public int Update <TModel>(Expression <Func <TModel, bool> > expression, dynamic updateValue) where TModel : CRL.IModel, new()
        {
            var properties = updateValue.GetType().GetProperties();
            var c          = new ParameCollection();

            foreach (var p in properties)
            {
                c.Add(p.Name, p.GetValue(updateValue));
            }
            return(Update(expression, c));
        }
Beispiel #6
0
        ParameCollection GetUpdateField <T>(T obj) where T : IModel, new()
        {
            var c      = new ParameCollection();
            var fields = TypeCache.GetProperties(typeof(T), true);

            if (obj.Changes.Count > 0)
            {
                foreach (var item in obj.Changes)
                {
                    var f = fields[item.Key];
                    if (f == null)
                    {
                        continue;
                    }
                    if (f.IsPrimaryKey || f.FieldType == Attribute.FieldType.虚拟字段)
                    {
                        continue;
                    }
                    c[item.Key] = item.Value;
                }
                return(c);
            }
            var origin = obj.OriginClone;

            if (origin == null)
            {
                throw new Exception("_originClone为空,请确认此对象是由查询创建");
            }
            CheckData(obj);

            foreach (var f in fields.Values)
            {
                if (f.IsPrimaryKey)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(f.VirtualField))
                {
                    continue;
                }
                var originValue  = f.GetValue(origin);
                var currentValue = f.GetValue(obj);
                if (!Object.Equals(originValue, currentValue))
                {
                    c.Add(f.Name, currentValue);
                }
            }
            return(c);
        }
Beispiel #7
0
        /// <summary>
        /// 按匿名对象更新
        /// </summary>
        /// <typeparam name="TOjbect"></typeparam>
        /// <param name="expression"></param>
        /// <param name="updateValue"></param>
        /// <returns></returns>
        public int Update <TOjbect>(Expression <Func <TModel, bool> > expression, TOjbect updateValue) where TOjbect : class
        {
            var properties = updateValue.GetType().GetProperties();
            var c          = new ParameCollection();

            foreach (var p in properties)
            {
                c.Add(p.Name, p.GetValue(updateValue));
            }
            var query = GetLambdaQuery();

            query.Where(expression);
            int n = Update(query, c);

            return(n);
        }
Beispiel #8
0
        internal ParameCollection GetUpdateField <TModel>(TModel obj) where TModel : IModel, new()
        {
            var c      = new ParameCollection();
            var fields = TypeCache.GetProperties(typeof(TModel), true);

            if (obj.GetChanges().Count > 0)//按手动指定更改
            {
                foreach (var item in obj.GetChanges())
                {
                    var key = item.Key.Replace("$", "");
                    var f   = fields[key];
                    if (f == null)
                    {
                        continue;
                    }
                    if (f.IsPrimaryKey || f.FieldType != Attribute.FieldType.数据库字段)
                    {
                        continue;
                    }
                    var value = item.Value;
                    //如果表示值为被追加 名称为$name
                    //使用Cumulation扩展方法后按此处理
                    if (key != item.Key)//按$name=name+'123123'
                    {
                        if (f.PropertyType == typeof(string))
                        {
                            value = string.Format("{0}+'{1}'", key, value);
                        }
                        else
                        {
                            value = string.Format("{0}+{1}", key, value);
                        }
                    }
                    c[item.Key] = value;
                }
                return(c);
            }
            //按对象对比
            var origin = obj.OriginClone;

            if (origin == null)
            {
                throw new CRLException("_originClone为空,请确认此对象是由查询创建");
            }
            foreach (var f in fields.Values)
            {
                if (f.IsPrimaryKey)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(f.VirtualField))
                {
                    continue;
                }
                var originValue  = f.GetValue(origin);
                var currentValue = f.GetValue(obj);
                if (!Object.Equals(originValue, currentValue))
                {
                    c.Add(f.MemberName, currentValue);
                }
            }
            if (c.Count() > 0)
            {
                CheckData(obj);
            }
            return(c);
        }
Beispiel #9
0
        ParameCollection GetUpdateField <TModel>(TModel obj) where TModel : IModel, new()
        {
            var c      = new ParameCollection();
            var fields = TypeCache.GetProperties(typeof(TModel), true);

            if (obj.Changes.Count > 0)
            {
                foreach (var item in obj.Changes)
                {
                    var key = item.Key.Replace("$", "");
                    var f   = fields[key];
                    if (f == null)
                    {
                        continue;
                    }
                    if (f.IsPrimaryKey || f.FieldType == Attribute.FieldType.虚拟字段)
                    {
                        continue;
                    }
                    var value = item.Value;
                    //如果表示值为被追求 名称为$name
                    if (key != item.Key)//按$name=name+'123123'
                    {
                        if (value.ToString().IsNumber())
                        {
                            value = string.Format("{0}+{1}", key, value);
                        }
                        else
                        {
                            value = string.Format("{0}+'{1}'", key, value);
                        }
                    }
                    c[item.Key] = value;
                }
                return(c);
            }
            var origin = obj.OriginClone;

            if (origin == null)
            {
                throw new Exception("_originClone为空,请确认此对象是由查询创建");
            }
            CheckData(obj);

            foreach (var f in fields.Values)
            {
                if (f.IsPrimaryKey)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(f.VirtualField))
                {
                    continue;
                }
                var originValue  = f.GetValue(origin);
                var currentValue = f.GetValue(obj);
                if (!Object.Equals(originValue, currentValue))
                {
                    c.Add(f.Name, currentValue);
                }
            }
            return(c);
        }