1 /// <summary> 2 /// The data extension. 3 /// </summary> 4 public static class DataExtension 5 { 6 /// <summary> 7 /// ToList 8 /// </summary> 9 /// <typeparam name="T">T</typeparam> 10 /// <param name="reader">reader</param> 11 /// <returns>T</returns> 12 public static List<T> ToList<T>(this IDataReader reader) where T : class, new() 13 { 14 var result = new List<T>(); 15 16 DataTable dt = reader.GetSchemaTable(); 17 try 18 { 19 while (reader.Read()) 20 { 21 var t = new T(); 22 23 if (dt != null) 24 { 25 foreach (DataRow dr in dt.Rows) 26 { 27 // 当前列名&属性名 28 string columnName = dr[0].ToString(); 29 PropertyInfo pro = typeof(T).GetProperty(columnName); 30 31 if (pro == null) 32 { 33 continue; 34 } 35 36 if (!pro.CanWrite) 37 { 38 continue; 39 } 40 41 pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null); 42 } 43 } 44 45 result.Add(t); 46 } 47 } 48 catch (System.Exception ex) 49 { 50 throw ex; 51 } 52 finally 53 { 54 if (!reader.IsClosed) 55 { 56 reader.Dispose(); 57 reader.Close(); 58 } 59 } 60 61 return result; 62 } 63 64 /// <summary> 65 /// ToList 66 /// </summary> 67 /// <typeparam name="T">T</typeparam> 68 /// <param name="dt">dt</param> 69 /// <returns>T</returns> 70 public static List<T> ToList<T>(this DataTable dt) where T : class, new() 71 { 72 var result = new List<T>(); 73 foreach (DataRow dr in dt.Rows) 74 { 75 var t = new T(); 76 try 77 { 78 foreach (DataColumn column in dt.Columns) 79 { 80 // 当前列名&属性名 81 string columnName = column.ColumnName; 82 PropertyInfo pro = typeof(T).GetProperty(columnName); 83 84 if (pro == null) 85 { 86 continue; 87 } 88 89 if (!pro.CanWrite) 90 { 91 continue; 92 } 93 94 pro.SetValue(t, ConvertExtension.ConvertHelper(dr[columnName], pro.PropertyType), null); 95 } 96 } 97 catch (System.Exception ex) 98 { 99 throw ex; 100 } 101 102 result.Add(t); 103 } 104 105 return result; 106 } 107 108 /// <summary> 109 /// ToList 110 /// </summary> 111 /// <typeparam name="T">T</typeparam> 112 /// <param name="ds">ds</param> 113 /// <returns>T</returns> 114 public static List<T> ToList<T>(this DataSet ds) where T : class, new() 115 { 116 return ds.Tables[0].ToList<T>(); 117 } 118 119 /// <summary> 120 /// ToList 121 /// </summary> 122 /// <typeparam name="T">T</typeparam> 123 /// <param name="ds">ds</param> 124 /// <param name="dataTableIndex">dataTableIndex</param> 125 /// <returns>T</returns> 126 public static List<T> ToList<T>(this DataSet ds, int dataTableIndex) where T : class, new() 127 { 128 return ds.Tables[dataTableIndex].ToList<T>(); 129 } 130 131 /// <summary> 132 /// ToModel 133 /// </summary> 134 /// <typeparam name="T">T</typeparam> 135 /// <param name="reader">reader</param> 136 /// <returns>T</returns> 137 public static T ToModel<T>(this IDataReader reader) where T : class, new() 138 { 139 var t = new T(); 140 DataTable dt = reader.GetSchemaTable(); 141 try 142 { 143 while (reader.Read()) 144 { 145 if (dt != null) 146 { 147 foreach (DataRow dr in dt.Rows) 148 { 149 // 当前列名&属性名 150 string columnName = dr[0].ToString(); 151 PropertyInfo pro = typeof(T).GetProperty(columnName); 152 153 if (pro == null) 154 { 155 continue; 156 } 157 158 if (!pro.CanWrite) 159 { 160 continue; 161 } 162 163 pro.SetValue(t, ConvertExtension.ConvertHelper(reader[columnName], pro.PropertyType), null); 164 } 165 } 166 } 167 } 168 catch (System.Exception ex) 169 { 170 throw ex; 171 } 172 finally 173 { 174 if (!reader.IsClosed) 175 { 176 reader.Dispose(); 177 reader.Close(); 178 } 179 } 180 181 return t; 182 } 183 184 /// <summary> 185 /// ToModel 186 /// </summary> 187 /// <typeparam name="T">T</typeparam> 188 /// <param name="dt">dt</param> 189 /// <returns>T</returns> 190 public static T ToModel<T>(this DataTable dt) where T : class, new() 191 { 192 var t = new T(); 193 if (dt.Rows.Count <= 0) 194 { 195 return t; 196 } 197 198 try 199 { 200 foreach (DataColumn column in dt.Columns) 201 { 202 // 当前列名&属性名 203 string columnName = column.ColumnName; 204 PropertyInfo pro = typeof(T).GetProperty(columnName); 205 if (pro == null) 206 { 207 continue; 208 } 209 210 if (!pro.CanWrite) 211 { 212 continue; 213 } 214 215 pro.SetValue(t, ConvertExtension.ConvertHelper(dt.Rows[0][columnName], pro.PropertyType), null); 216 } 217 } 218 catch (System.Exception ex) 219 { 220 throw ex; 221 } 222 223 return t; 224 } 225 226 /// <summary> 227 /// ToModel 228 /// </summary> 229 /// <typeparam name="T">T</typeparam> 230 /// <param name="ds">ds</param> 231 /// <param name="dataTableIndex">dataTableIndex</param> 232 /// <returns>T</returns> 233 public static T ToModel<T>(this DataSet ds, int dataTableIndex = 0) where T : class, new() 234 { 235 return ds.Tables[0].ToModel<T>(); 236 } 237 }
DataExtension
1 /// <summary> 2 /// The convert extension. 3 /// </summary> 4 public static class ConvertExtension 5 { 6 /// <summary> 7 /// The convert helper. 8 /// </summary> 9 /// <param name="value"> 10 /// The value. 11 /// </param> 12 /// <param name="conversionType"> 13 /// The conversion type. 14 /// </param> 15 /// <returns> 16 /// The <see cref="object"/>. 17 /// </returns> 18 public static object ConvertHelper(object value, Type conversionType) 19 { 20 Type nullableType = Nullable.GetUnderlyingType(conversionType); 21 22 // 判断当前类型是否可为 null 23 if (nullableType != null) 24 { 25 if (value == DBNull.Value) 26 { 27 return null; 28 } 29 30 // 若是枚举 则先转换为枚举 31 if (nullableType.IsEnum) 32 { 33 value = System.Enum.Parse(nullableType, value.ToString()); 34 } 35 36 return Convert.ChangeType(value, nullableType); 37 } 38 39 if (conversionType.IsEnum) 40 { 41 return System.Enum.Parse(conversionType, value.ToString()); 42 } 43 44 return Convert.ChangeType(value, conversionType); 45 } 46 47 /// <summary> 48 /// The convert to decimal null. 49 /// </summary> 50 /// <param name="targetObj"> 51 /// The target obj. 52 /// </param> 53 /// <returns> 54 /// The <see cref="decimal"/>. 55 /// </returns> 56 public static decimal? ConvertToDecimalNull(object targetObj) 57 { 58 if (targetObj == null || targetObj == DBNull.Value) 59 { 60 return null; 61 } 62 63 return Convert.ToDecimal(targetObj); 64 } 65 66 /// <summary> 67 /// The convert to int null. 68 /// </summary> 69 /// <param name="targetObj"> 70 /// The target obj. 71 /// </param> 72 /// <returns> 73 /// The <see cref="int"/>. 74 /// </returns> 75 public static int? ConvertToIntNull(object targetObj) 76 { 77 if (targetObj == null || targetObj == DBNull.Value) 78 { 79 return null; 80 } 81 82 return Convert.ToInt32(targetObj); 83 } 84 85 /// <summary> 86 /// The convert to string. 87 /// </summary> 88 /// <param name="obj"> 89 /// The obj. 90 /// </param> 91 /// <returns> 92 /// The <see cref="string"/>. 93 /// </returns> 94 public static string ConvertToString(object obj) 95 { 96 return obj == null ? string.Empty : obj.ToString(); 97 } 98 99 /// <summary> 100 /// 将泛类型集合List类转换成DataTable 101 /// </summary> 102 /// <param name="entitys">泛类型集合</param> 103 /// <typeparam name="T">T</typeparam> 104 /// <returns>DataTable</returns> 105 public static DataTable ListToDataTable<T>(List<T> entitys) 106 { 107 // 检查实体集合不能为空 108 if (entitys == null || entitys.Count < 1) 109 { 110 throw new System.Exception("需转换的集合为空"); 111 } 112 113 // 取出第一个实体的所有Propertie 114 Type entityType = entitys[0].GetType(); 115 PropertyInfo[] entityProperties = entityType.GetProperties(); 116 117 // 生成DataTable的structure 118 // 生产代码中,应将生成的DataTable结构Cache起来,此处略 119 DataTable dt = new DataTable(); 120 foreach (PropertyInfo t in entityProperties) 121 { 122 // dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType); 123 dt.Columns.Add(t.Name); 124 } 125 126 // 将所有entity添加到DataTable中 127 foreach (object entity in entitys) 128 { 129 // 检查所有的的实体都为同一类型 130 if (entity.GetType() != entityType) 131 { 132 throw new System.Exception("要转换的集合元素类型不一致"); 133 } 134 135 object[] entityValues = new object[entityProperties.Length]; 136 for (int i = 0; i < entityProperties.Length; i++) 137 { 138 entityValues[i] = entityProperties[i].GetValue(entity, null); 139 } 140 141 dt.Rows.Add(entityValues); 142 } 143 144 return dt; 145 } 146 147 /// <summary> 148 /// 转换中文星期 149 /// </summary> 150 /// <param name="dt">The dt.</param> 151 /// <returns>Week.</returns> 152 public static Week ConverToWeekByZHCN(this DateTime dt) 153 { 154 return (Week)dt.DayOfWeek; 155 } 156 157 /// <summary> 158 /// 四舍五入保留2位小数(中国式) 159 /// </summary> 160 /// <param name="d"></param> 161 /// <returns></returns> 162 public static decimal DecimalTwoPlaces(this decimal d) 163 { 164 return Math.Round(d, 2, MidpointRounding.AwayFromZero); 165 } 166 }
ConvertExtension
将 IDataReader DataSet DataTable 转化成 List<T> Or T类型
上面是转化代码
时间: 2024-10-11 04:52:57