关于DataTable与IList和List泛型集合的相互转换在网上总结

我在做amchart的还有微软相关的chart控件时发现绑定使用Datatabale不能绑定 但是支持DataSet 和泛型集合;于是谢谢网上好友的帮助;自己做了下总结

自己弄了一些集合转化的文章;

对于技术方面的理论我不需多言;

主要是是通过映射命名空间;使用Linq的相关查询;和Type类获取列名;使用泛型转化为实体类后放到集合:

代码如下:

 1 publicstaticclass ConvertTolistInfo
2 {
3 ///<summary>
4 /// DataTable 转换为List 集合
5 ///</summary>
6 ///<typeparam name="T">类型</typeparam>
7 ///<param name="dt">DataTable</param>
8 ///<returns></returns>
9 publicstatic List<TResult> ToList<TResult>(this DataTable dt) where TResult : class,new()
10 {
11 List<PropertyInfo> prlist =new List<PropertyInfo>();
12 Type t =typeof(TResult);
13 Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) !=-1)prlist.Add(p); });
14 List<TResult> objlist =new List<TResult>();
15 foreach (DataRow row in dt.Rows)
16 {
17 TResult obj =new TResult();
18 prlist.ForEach(p =>
19 {
20 if (row[p.Name] != DBNull.Value)
21 p.SetValue(obj, row[p.Name], null);
22 });
23 objlist.Add(obj);
24 }
25 return objlist;
26 }
27 ///<summary>
28 /// 转换为一个DataTable
29 ///</summary>
30 ///<typeparam name="T"></typeparam>
31 ///<param name="value"></param>
32 ///<returns></returns>
33 publicstatic DataTable ToDataTable<T>(this IEnumerable<T> value) where T : class
34 {
35 List<PropertyInfo> plist =new List<PropertyInfo>();
36 Type t =typeof(T);
37 //T type = new T();
38 //Type columen = type.GetType();这种方式与 where T :new()这种方式使用 否则不适用;
39 DataTable dt =new DataTable();
40 Array.ForEach<PropertyInfo>(t.GetProperties(), p =>
41 {
42 plist.Add(p);
43 dt.Columns.Add(p.Name, p.PropertyType);
44 });
45 foreach (var item in value)
46 {
47 //创建一个DataRow实例
48 DataRow row = dt.NewRow();
49 //给row 赋值
50 plist.ForEach(p => row[p.Name] = p.GetValue(item, null));
51 dt.Rows.Add(row);
52 }
53 return dt;
54 }
55 }

  二,构造泛型类:

/// <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;
}

}

  

原文地址:https://www.cnblogs.com/qiliping/p/2120858.html