关于DataTable与IList和List泛型集合的相互转换在网上总结
2011-07-29 11:59
627 查看
我在做amchart的还有微软相关的chart控件时发现绑定使用Datatabale不能绑定 但是支持DataSet 和泛型集合;于是谢谢网上好友的帮助;自己做了下总结
自己弄了一些集合转化的文章;
对于技术方面的理论我不需多言;
主要是是通过映射命名空间;使用Linq的相关查询;和Type类获取列名;使用泛型转化为实体类后放到集合:
代码如下:
二,构造泛型类:
自己弄了一些集合转化的文章;
对于技术方面的理论我不需多言;
主要是是通过映射命名空间;使用Linq的相关查询;和Type类获取列名;使用泛型转化为实体类后放到集合:
代码如下:
publicstaticclass ConvertTolistInfo { ///<summary> /// DataTable 转换为List 集合 ///</summary> ///<typeparam name="T">类型</typeparam> ///<param name="dt">DataTable</param> ///<returns></returns> publicstatic List<TResult> ToList<TResult>(this DataTable dt) where TResult : class,new() { List<PropertyInfo> prlist =new List<PropertyInfo>(); Type t =typeof(TResult); Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) !=-1)prlist.Add(p); }); List<TResult> objlist =new List<TResult>(); foreach (DataRow row in dt.Rows) { TResult obj =new TResult(); prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(obj, row[p.Name], null); }); objlist.Add(obj); } return objlist; } ///<summary> /// 转换为一个DataTable ///</summary> ///<typeparam name="T"></typeparam> ///<param name="value"></param> ///<returns></returns> publicstatic DataTable ToDataTable<T>(this IEnumerable<T> value) where T : class { List<PropertyInfo> plist =new List<PropertyInfo>(); Type t =typeof(T); //T type = new T(); //Type columen = type.GetType();这种方式与 where T :new()这种方式使用 否则不适用; DataTable dt =new DataTable(); Array.ForEach<PropertyInfo>(t.GetProperties(), p => { plist.Add(p); dt.Columns.Add(p.Name, p.PropertyType); }); foreach (var item in value) { //创建一个DataRow实例 DataRow row = dt.NewRow(); //给row 赋值 plist.ForEach(p => row[p.Name] = p.GetValue(item, null)); dt.Rows.Add(row); } return dt; } }
二,构造泛型类:
/// <summary> /// 实体转换辅助类 /// </summary> publicclass ModelConvertHelper<T>where T : new() { publicstatic IList<T> ConvertToModel(DataTable dt) { // 定义集合 IList<T> ts =new List<T>(); // 获得此模型的类型 Type type =typeof(T); string tempName =""; foreach (DataRow dr in dt.Rows) { T t =new T(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; // 检查DataTable是否包含此列 if (dt.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } /// <summary> /// 提供将DataTable类型对象转换为List集合 /// </summary> /// <param name="table"></param> /// <returns></returns> publicstatic List<T> ConvertToList<T>(DataTable table) where T : new() { //置为垃圾对象 List<T> list =null; if (table !=null) { DataColumnCollection columns = table.Columns; int columnCount = columns.Count; T type =new T(); Type columnType = type.GetType(); PropertyInfo[] properties = columnType.GetProperties(); if (properties.Length == columnCount) { list =new List<T>(); foreach (DataRow currentRow in table.Rows) { for (int i =0; i < columnCount; i++) { for (int j =0; j < properties.Length; j++) { if (columns[i].ColumnName == properties[j].Name) { properties[j].SetValue(type, currentRow[i], null); } } } list.Add(type); type =new T(); } } else { list =null; } } else { thrownew ArgumentNullException("参数不能为空"); } return list; } }
相关文章推荐
- DataSet、DataTable转换List(泛型集合与DataSet互相转换 )
- List转DataTable(反射) ; 将泛型集合类转换成DataTable ; 将集合类转换成DataTable
- 将DataTable转换成泛型集合IList<>
- DataTable转换list泛型集合(反射)
- 将DataTable转换成List<T>泛型集合助手类
- C#控制台基础 List泛型集合与对应的数组相互转换实现代码
- C#控制台基础 List泛型集合与对应的数组相互转换
- 关于List,字符串,数组的相互转换总结
- 将一个DataTable转换成一个List<T>的泛型集合
- 将一个DataTable转换成一个List<T>的泛型集合
- 将泛型集合类(List<T>)转换成DataTable,返回的DataTable所有列的数据类型为string型,且列顺序与字符串提供的字段顺序一致
- C#中string[]数组和list<string>泛型的相互转换 【转】
- Java 集合转换(数组、List、Set、Map相互转换)
- 关于数组和List之间相互转换的方法
- List<T> 和DataTable的相互转换
- 关于 List 泛型集合访问方法
- 关于数组和List之间相互转换的方法
- .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法 【整理】
- C#中string[]数组和list<string>泛型的相互转换
- DataTable 和List 相互转换