public Tuple<string,OracleParameter[]> StuParam(EntTest model) { string sql = string.Empty; OracleParameter [] param=new OracleParameter[model.GetType().GetProperties().Count()]; int index = 0; if (!string.IsNullOrEmpty(model.Id.ToString())) { //sql += string.Format(" id=:{0}, ", EntTest.DB_ID); param[index] = new OracleParameter(":" + EntTest.DB_ID, model.Id); index++; } if (!string.IsNullOrEmpty(model.Name)) { sql += string.Format("name=:{0},",EntTest.DB_NAME); param[index] = new OracleParameter(":"+EntTest.DB_NAME,model.Name); index++; } if (!string.IsNullOrEmpty(model.Age.ToString())) { sql += string.Format("age=:{0},",EntTest.DB_AGE); param[index] = new OracleParameter(":"+EntTest.DB_AGE,model.Age); index++; } if (!string.IsNullOrEmpty(model.Sex)) { sql += string.Format("sex=:{0},",EntTest.DB_SEX); param[index] = new OracleParameter(":"+EntTest.DB_SEX,model.Sex); index++; } if (!string.IsNullOrEmpty(model.Address)) { sql += string.Format("address=:{0},",EntTest.DB_ADDRESS); param[index] = new OracleParameter(":"+EntTest.DB_ADDRESS, model.Sex); index++; } if (!string.IsNullOrEmpty(model.Hobby)) { sql += string.Format("hobby=:{0},",EntTest.DB_HOBBY); param[index] = new OracleParameter(":"+EntTest.DB_HOBBY, model.Hobby); index++; } if (!string.IsNullOrEmpty(model.Class)) { sql += string.Format("class=:{0},",EntTest.DB_CLASS); param[index] = new OracleParameter(":"+EntTest.DB_CLASS, model.Class); index++; } if (!string.IsNullOrEmpty(model.UpdateTime.ToString())) { sql += string.Format("update_time=:{0},",EntTest.DB_UPDATE_TIME); param[index] = new OracleParameter(":"+EntTest.DB_UPDATE_TIME, model.UpdateTime); index++; } if (!string.IsNullOrEmpty(model.UpdateUser)) { sql += string.Format("update_user=:{0},", EntTest.DB_UPDATE_USER); param[index] = new OracleParameter(":" + EntTest.DB_UPDATE_USER, model.UpdateUser); index++; } if (!string.IsNullOrEmpty(model.CreateTime.ToString())) { sql += string.Format("create_time=:{0},", EntTest.DB_CREATE_TIME); param[index] = new OracleParameter(":" + EntTest.DB_CREATE_TIME, model.CreateTime); index++; } if (!string.IsNullOrEmpty(model.CreateUser)) { sql += string.Format("create_user=:{0},", EntTest.DB_CREATE_USER); param[index] = new OracleParameter(":" + EntTest.DB_CREATE_USER, model.CreateUser); index++; } sql=sql.Remove(sql.LastIndexOf(","),1); //OracleParameter[] s=param.ToArray<OracleParameter>().Select()(p=>p!=null); return new Tuple<string,OracleParameter[]>(sql,param); }
我想将根据传过来的实体 判断实体EntTest的属性是否为空
如果不为空的话
然后的问题是:OracleParameter数组有空元素值为null 执行修改操作会报错
因为EntTest的属性总素为数组长度
没有把数组的所有索引下的值填满
只有5个有值
所以我想删除除了这5个之外的数组元素!
希望大神教教我,感激不尽!
另外我希望这个数组长度不要这样写死,有更好的做法也可以给小弟提提!
既然都用上反射了,直接反射就好了。
可是怎么删除索引5之后的空的数组元素呢!
@waitforjuan: 用 List<OracleParameter>吧。最后ToArray 不用关系有多少个。
@长蘑菇星人:是的 我刚刚就是用这个解决的 只不过还需要for循环添加到OracleParameter[]数组里面去,有点代码累赘了
@waitforjuan: 。。。ToArray 就好了啊。