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