Esempio n. 1
0
        public static KeyValuePair <string, KeyValuePair <string[], object[]> > GetPropertiesDelete(Expression <Func <TEntity, bool> > where)
        {
            ConditionBuilder whereBuilder = new ConditionBuilder((typeof(TEntity)).ToString());

            whereBuilder.Build(where.Body);
            List <string> param  = new List <string>();
            List <object> pvalue = new List <object>();

            param.AddRange(whereBuilder.WhereParam);
            pvalue.AddRange(whereBuilder.Arguments);
            string sql = string.Format("delete from  {0} WHERE {1}", (typeof(TEntity)).ToString().Split('.').Last(), whereBuilder.Condition);
            KeyValuePair <string, KeyValuePair <string[], object[]> > kvp = new KeyValuePair <string, KeyValuePair <string[], object[]> >(sql, new KeyValuePair <string[], object[]>(param.ToArray(), pvalue.ToArray()));

            return(kvp);
        }
Esempio n. 2
0
        public static KeyValuePair <string, KeyValuePair <string[], object[]> > GetPropertiesUpdate(Expression <Func <TEntity> > expression, Expression <Func <TEntity, bool> > where)
        {
            ConditionBuilder whereBuilder = new ConditionBuilder((typeof(TEntity)).ToString());

            whereBuilder.Build(where.Body);
            var updateExp  = expression.Body as MemberInitExpression;
            var updateList = updateExp.Bindings.Cast <MemberAssignment>().Select(a => new
            {
                Name  = a.Member.Name,
                Value = GetMemeberValue(a)
            });
            StringBuilder sqlUpdate = new StringBuilder();
            int           i         = 0;
            List <string> param     = new List <string>();
            List <object> pvalue    = new List <object>();

            foreach (var item in updateList)
            {
                string column = item.Name;
                string pname  = string.Format("p{0}__{1}", i.ToString(), column);
                if (!string.IsNullOrEmpty(column))
                {
                    sqlUpdate.AppendFormat(" {0} = @{1},", column, pname);
                    param.Add(pname);
                    //zsp  空时间更新为空
                    if (item.Value != null && item.Value is DateTime && ((DateTime)item.Value) == DateTime.MinValue)
                    {
                        pvalue.Add(null);
                    }
                    else
                    {
                        pvalue.Add(item.Value);
                    }
                    i++;
                }
            }
            param.AddRange(whereBuilder.WhereParam);
            pvalue.AddRange(whereBuilder.Arguments);
            string sql = string.Format("UPDATE {0} SET {1} WHERE {2}", (typeof(TEntity)).ToString().Split('.').Last(), sqlUpdate.ToString().TrimEnd(','), whereBuilder.Condition);
            KeyValuePair <string, KeyValuePair <string[], object[]> > kvp = new KeyValuePair <string, KeyValuePair <string[], object[]> >(sql, new KeyValuePair <string[], object[]>(param.ToArray(), pvalue.ToArray()));

            return(kvp);
        }