/// <summary> /// 实体列表转DataTable /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="modelList">实体列表</param> /// <returns></returns> public static DataTable ModelList2DataTable <T>(List <T> modelList) { if (modelList == null) { return(null); } DataTable dataTable = new DataTable(); var model = modelList[0]; Type t = model.GetType();//获得该类的Type dataTable.TableName = ModelDAL.GetTableName(t); //创建表结构、设置字段信息 bool hasStar = false; foreach (PropertyInfo pi in t.GetProperties()) { DataColumn dataColumn = new DataColumn(); ModelAttribute attr = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值 if (attr != null) { dataColumn.ColumnName = attr.ColumnName; dataColumn.DataType = TypeUtil.GetType(attr.ColumnType); dataColumn.Caption = attr.ColumnTitle; if (attr.IsPrimaryKey) { dataColumn.Unique = true; } } else { object value = pi.GetValue(model, null); dataColumn.ColumnName = pi.Name; if (value != null) { dataColumn.DataType = value.GetType(); } //else //{ // dataColumn.DataType = typeof(string); //} } if (Common.Star_SelectAllColumn.Equals(pi.Name)) { hasStar = true; continue; } dataTable.Columns.Add(dataColumn); } //添加行数据 foreach (var m in modelList) { object[] values = new object[t.GetProperties().Length - (hasStar ? 1 : 0)]; int idx = 0; foreach (PropertyInfo pi in t.GetProperties()) { if (Common.Star_SelectAllColumn.Equals(pi.Name)) { continue; } object value = pi.GetValue(m, null); ModelAttribute attr = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值 //if (attr != null && !string.IsNullOrEmpty(attr.ColumnType)) //{ // value = pi.GetValue(model, null); //} //else //{ // value = pi.GetValue(model, null); //} if (attr.ColumnType == ColumnType.datetimeType && (null == value || (System.DateTime)value == default(System.DateTime))) { //空时间类型或空值,不写入数据库 continue; } values[idx] = value; idx++; } dataTable.Rows.Add(values); } return(dataTable); }
/// <summary> /// 实体列表转DataTable /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="modelList">实体列表</param> /// <returns></returns> public static DataTable ModelList2DataTable <T>(List <T> modelList) { if (modelList == null) { return(null); } DataTable dataTable = new DataTable(); var model = modelList[0]; Type t = model.GetType();//获得该类的Type //创建表结构、设置字段信息 foreach (PropertyInfo pi in t.GetProperties()) { DataColumn dataColumn = new DataColumn(); ModelAttribute attr = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值 if (attr != null) { dataColumn.ColumnName = attr.ColumnName; dataColumn.DataType = TypeUtil.GetType(attr.ColumnType); dataColumn.Caption = attr.ColumnTitle; if (attr.IsPrimaryKey) { dataColumn.Unique = true; } } else { object value = pi.GetValue(model, null); dataColumn.ColumnName = pi.Name; if (value != null) { dataColumn.DataType = value.GetType(); } //else //{ // dataColumn.DataType = typeof(string); //} } dataTable.Columns.Add(dataColumn); } //添加行数据 foreach (var m in modelList) { object[] values = new object[t.GetProperties().Length]; int idx = 0; foreach (PropertyInfo pi in t.GetProperties()) { object value = pi.GetValue(model, null); //ModelAttribute attr = (ModelAttribute)Attribute.GetCustomAttribute(pi, typeof(ModelAttribute));// 属性值 //if (attr != null && !string.IsNullOrEmpty(attr.ColumnType)) //{ // value = pi.GetValue(model, null); //} //else //{ // value = pi.GetValue(model, null); //} values[idx] = value; idx++; } dataTable.Rows.Add(values); } return(dataTable); }