首页 > 代码库 > C# Table转换List 或List转换Table

C# Table转换List 或List转换Table

  1 public static class TableListHelper  2     {  3   4         /// <summary>    5         /// 转化一个DataTable    6         /// </summary>    7         /// <typeparam name="T"></typeparam>    8         /// <param name="list"></param>    9         /// <returns></returns>   10         public static DataTable ToDataTable<T>(this IEnumerable<T> list) 11         { 12             //创建属性的集合   13             List<PropertyInfo> pList = new List<PropertyInfo>(); 14             //获得反射的入口   15             Type type = typeof(T); 16             DataTable dt = new DataTable(); 17             //把所有的public属性加入到集合 并添加DataTable的列   18             Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); }); 19             foreach (var item in list) 20             { 21                 //创建一个DataRow实例   22                 DataRow row = dt.NewRow(); 23                 //给row 赋值   24                 pList.ForEach(p => row[p.Name] = p.GetValue(item, null)); 25                 //加入到DataTable   26                 dt.Rows.Add(row); 27             } 28             return dt; 29         } 30  31  32         /// <summary>   33         /// DataTable 转换为List 集合   34         /// </summary>   35         /// <typeparam name="TResult">类型</typeparam>   36         /// <param name="dt">DataTable</param>   37         /// <returns></returns>   38         public static List<T> ToList<T>(this DataTable dt) where T : class, new() 39         { 40             //创建一个属性的列表   41             List<PropertyInfo> prlist = new List<PropertyInfo>(); 42             //获取TResult的类型实例  反射的入口   43  44             Type t = typeof(T); 45  46             //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表    47             Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); }); 48  49             //创建返回的集合   50  51             List<T> oblist = new List<T>(); 52  53             foreach (DataRow row in dt.Rows) 54             { 55                 //创建TResult的实例   56                 T ob = new T(); 57                 //找到对应的数据  并赋值   58                 prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); }); 59                 //放入到返回的集合中.   60                 oblist.Add(ob); 61             } 62             return oblist; 63         } 64  65  66         /// <summary>   67         /// 将集合类转换成DataTable   68         /// </summary>   69         /// <param name="list">集合</param>   70         /// <returns></returns>   71         public static DataTable ToDataTableTow(IList list) 72         { 73             DataTable result = new DataTable(); 74             if (list.Count > 0) 75             { 76                 PropertyInfo[] propertys = list[0].GetType().GetProperties(); 77  78                 foreach (PropertyInfo pi in propertys) 79                 { 80                     result.Columns.Add(pi.Name, pi.PropertyType); 81                 } 82                 for (int i = 0; i < list.Count; i++) 83                 { 84                     ArrayList tempList = new ArrayList(); 85                     foreach (PropertyInfo pi in propertys) 86                     { 87                         object obj = pi.GetValue(list[i], null); 88                         tempList.Add(obj); 89                     } 90                     object[] array = tempList.ToArray(); 91                     result.LoadDataRow(array, true); 92                 } 93             } 94             return result; 95         } 96  97  98         /// <summary>   99         /// 将泛型集合类转换成DataTable  100         /// </summary>  101         /// <typeparam name="T">集合项类型</typeparam>  102         /// <param name="list">集合</param>  103         /// <param name="propertyName">需要返回的列的列名</param>  104         /// <returns>数据集(表)</returns>  105         public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)106         {107             List<string> propertyNameList = new List<string>();108             if (propertyName != null)109                 propertyNameList.AddRange(propertyName);110             DataTable result = new DataTable();111             if (list.Count > 0)112             {113                 PropertyInfo[] propertys = list[0].GetType().GetProperties();114                 foreach (PropertyInfo pi in propertys)115                 {116                     if (propertyNameList.Count == 0)117                     {118                         result.Columns.Add(pi.Name, pi.PropertyType);119                     }120                     else121                     {122                         if (propertyNameList.Contains(pi.Name))123                             result.Columns.Add(pi.Name, pi.PropertyType);124                     }125                 }126 127                 for (int i = 0; i < list.Count; i++)128                 {129                     ArrayList tempList = new ArrayList();130                     foreach (PropertyInfo pi in propertys)131                     {132                         if (propertyNameList.Count == 0)133                         {134                             object obj = pi.GetValue(list[i], null);135                             tempList.Add(obj);136                         }137                         else138                         {139                             if (propertyNameList.Contains(pi.Name))140                             {141                                 object obj = pi.GetValue(list[i], null);142                                 tempList.Add(obj);143                             }144                         }145                     }146                     object[] array = tempList.ToArray();147                     result.LoadDataRow(array, true);148                 }149             }150             return result;151         }152 153     }
View Code

 

C# Table转换List 或List转换Table