对DataSet,DataRow,DateTable转换成相应的模型
生活随笔
收集整理的這篇文章主要介紹了
对DataSet,DataRow,DateTable转换成相应的模型
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/// <summary>/// DataRow 轉(zhuǎn)成 模型/// </summary>/// <typeparam name="T"></typeparam>/// <param name="dr"></param>/// <returns></returns>public static T ToModel<T>(this DataRow dr) where T : class, new(){T ob = new T();if (dr != null){Type vType = typeof(T);//創(chuàng)建一個(gè)屬性的列表PropertyInfo[] prlist = vType.GetProperties();DataColumnCollection vDataCoulumns = dr.Table.Columns;try{foreach (PropertyInfo vProInfo in prlist){if (vDataCoulumns.IndexOf(vProInfo.Name) >= 0 && dr[vProInfo.Name] != DBNull.Value){vProInfo.SetValue(ob, dr[vProInfo.Name], null);}}}catch (Exception ex){}}return ob;}/// <summary>/// DataTable 轉(zhuǎn)換為List 集合/// </summary>/// <typeparam name="T">類型</typeparam>/// <param name="dt">DataTable</param>/// <returns></returns>public static List<T> ToList<T>(this DataTable dt) where T : class, new(){//創(chuàng)建一個(gè)屬性的列表List<PropertyInfo> prlist = new List<PropertyInfo>();//獲取TResult的類型實(shí)例 ?反射的入口Type t = typeof(T);//獲得TResult 的所有的Public 屬性 并找出TResult屬性和DataTable的列名稱相同的屬性(PropertyInfo) 并加入到屬性列表?Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });//創(chuàng)建返回的集合List<T> oblist = new List<T>();if (dt != null){foreach (DataRow row in dt.Rows){try{//創(chuàng)建TResult的實(shí)例T ob = new T();//找到對應(yīng)的數(shù)據(jù) ?并賦值prlist.ForEach(p =>{if (dt.Columns.IndexOf(p.Name) >= 0 && row[p.Name] != DBNull.Value){try{//如果是泛型if (p.PropertyType.ToString().IndexOf("System.Nullable") > -1){string types = p.PropertyType.ToString().Substring(p.PropertyType.ToString().IndexOf("[") + 1);types = types.Substring(0, types.Length - 1);Type typeinfo = Type.GetType(types);p.SetValue(ob, Convert.ChangeType(row[p.Name], typeinfo), null);}else{p.SetValue(ob, Convert.ChangeType(row[p.Name], p.PropertyType), null);//類型轉(zhuǎn)換
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }}catch (Exception ex){LogUtility.WriteLogForExcepiton(ex);}}});//放入到返回的集合中.
? ? ? ? ? ? ? ? ? ? ? ? oblist.Add(ob);}catch (Exception ex){}}}return oblist;}
/// <summary> /// DataSet轉(zhuǎn)換為泛型集合 /// </summary> /// <typeparam name="T">泛型類型</typeparam> /// <param name="ds">DataSet數(shù)據(jù)集</param> /// <param name="tableIndex">待轉(zhuǎn)換數(shù)據(jù)表索引,默認(rèn)第0張表</param> /// <returns>返回泛型集合</returns> public static IList<T> ToList<T>(this DataSet ds, int tableIndex = 0){if (ds == null || ds.Tables.Count < 0) return null;if (tableIndex > ds.Tables.Count - 1)return null;if (tableIndex < 0)tableIndex = 0;DataTable dt = ds.Tables[tableIndex];// 返回值初始化 IList<T> result = new List<T>();for (int j = 0; j < dt.Rows.Count; j++){T _t = (T)Activator.CreateInstance(typeof(T));PropertyInfo[] propertys = _t.GetType().GetProperties();foreach (PropertyInfo pi in propertys){for (int i = 0; i < dt.Columns.Count; i++){// 屬性與字段名稱一致的進(jìn)行賦值 if (pi.Name.Equals(dt.Columns[i].ColumnName)){// 數(shù)據(jù)庫NULL值單獨(dú)處理 if (dt.Rows[j][i] != DBNull.Value)pi.SetValue(_t, dt.Rows[j][i], null);elsepi.SetValue(_t, null, null);break;}}}result.Add(_t);}return result;}/// <summary> /// DataSet轉(zhuǎn)換為泛型集合 /// </summary> /// <typeparam name="T">泛型類型</typeparam> /// <param name="ds">DataSet數(shù)據(jù)集</param> /// <param name="tableName">待轉(zhuǎn)換數(shù)據(jù)表名稱,名稱為空時(shí)默認(rèn)第0張表</param> /// <returns>返回泛型集合</returns> public static IList<T> ToList<T>(this DataSet ds, string tableName){int _TableIndex = 0;if (ds == null || ds.Tables.Count < 0)return null;if (string.IsNullOrEmpty(tableName))return ToList<T>(ds, 0);for (int i = 0; i < ds.Tables.Count; i++){// 獲取Table名稱在Tables集合中的索引值 if (ds.Tables[i].TableName.Equals(tableName)){_TableIndex = i;break;}}return ToList<T>(ds, _TableIndex);}
/// <summary> /// DataSet轉(zhuǎn)換為泛型集合 /// </summary> /// <typeparam name="T">泛型類型</typeparam> /// <param name="ds">DataSet數(shù)據(jù)集</param> /// <param name="tableIndex">待轉(zhuǎn)換數(shù)據(jù)表索引,默認(rèn)第0張表</param> /// <returns>返回泛型集合</returns> public static IList<T> ToList<T>(this DataSet ds, int tableIndex = 0){if (ds == null || ds.Tables.Count < 0) return null;if (tableIndex > ds.Tables.Count - 1)return null;if (tableIndex < 0)tableIndex = 0;DataTable dt = ds.Tables[tableIndex];// 返回值初始化 IList<T> result = new List<T>();for (int j = 0; j < dt.Rows.Count; j++){T _t = (T)Activator.CreateInstance(typeof(T));PropertyInfo[] propertys = _t.GetType().GetProperties();foreach (PropertyInfo pi in propertys){for (int i = 0; i < dt.Columns.Count; i++){// 屬性與字段名稱一致的進(jìn)行賦值 if (pi.Name.Equals(dt.Columns[i].ColumnName)){// 數(shù)據(jù)庫NULL值單獨(dú)處理 if (dt.Rows[j][i] != DBNull.Value)pi.SetValue(_t, dt.Rows[j][i], null);elsepi.SetValue(_t, null, null);break;}}}result.Add(_t);}return result;}/// <summary> /// DataSet轉(zhuǎn)換為泛型集合 /// </summary> /// <typeparam name="T">泛型類型</typeparam> /// <param name="ds">DataSet數(shù)據(jù)集</param> /// <param name="tableName">待轉(zhuǎn)換數(shù)據(jù)表名稱,名稱為空時(shí)默認(rèn)第0張表</param> /// <returns>返回泛型集合</returns> public static IList<T> ToList<T>(this DataSet ds, string tableName){int _TableIndex = 0;if (ds == null || ds.Tables.Count < 0)return null;if (string.IsNullOrEmpty(tableName))return ToList<T>(ds, 0);for (int i = 0; i < ds.Tables.Count; i++){// 獲取Table名稱在Tables集合中的索引值 if (ds.Tables[i].TableName.Equals(tableName)){_TableIndex = i;break;}}return ToList<T>(ds, _TableIndex);}
?
??
轉(zhuǎn)載于:https://www.cnblogs.com/Kendy/p/10271642.html
總結(jié)
以上是生活随笔為你收集整理的对DataSet,DataRow,DateTable转换成相应的模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 196℃到80℃ 安徽首例“康博刀”手术
- 下一篇: Navicat使用教程:使用Navica