DataTable与List集合转化

本文介绍了一种在C#中高效实现DataTable与List对象互相转换的方法。通过具体代码示例,展示了如何将DataTable转换为泛型List,反之亦然。此方法适用于数据库查询结果的快速处理与展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用于数据库查询数据转换、批量数据操作转换、导入导出Excel转换

 1 public static class DataTableHelper
 2 {
 3     /// <summary>
 4     /// 将DataTable对象转换成List对象
 5     /// </summary>
 6     /// <typeparam name="T">元数据类型</typeparam>
 7     /// <param name="table">DataTable类型的对象</param>
 8     /// <returns>返回List对象</returns>
 9     public static IList<T> ConvertTo<T>(DataTable table)
10     {
11         if (table == null)
12             return null;
13 
14         List<DataRow> rows = table.Rows.Cast<DataRow>().ToList();
15 
16         return ConvertTo<T>(rows);
17     }
18 
19     public static IList<T> ConvertTo<T>(IList<DataRow> rows)
20     {
21         IList<T> list = new List<T>();
22         if (rows != null)
23         {
24             foreach (DataRow row in rows.Cast<DataRow>().ToList())
25             {
26                 T obj = default(T);
27                 obj = Activator.CreateInstance<T>();
28                 foreach (DataColumn column in row.Table.Columns)
29                 {
30                     var columnName = column.ColumnName;
31                     var prop = obj.GetType().GetProperty(columnName);
32                     if (prop == null) continue;
33                     var value = (row[columnName] is DBNull) ? null : row[columnName];
34                     if (prop.CanWrite)
35                         prop.SetValue(obj, value, null);
36                 }
37                 list.Add(obj);
38             }
39         }
40         return list;
41     }
42 
43     /// <summary>
44     /// 将IList对象转换成DataTable
45     /// </summary>
46     /// <typeparam name="T">类型</typeparam>
47     /// <param name="list">源数据</param>
48     /// <returns>返回DataTable</returns>
49     public static DataTable ConvertTo<T>(IList<T> list)
50     {
51         DataTable dt = new DataTable();
52         if (list.Count == 0)
53             return dt;
54 
55         //创建table类型
56         var t=Activator.CreateInstance<T>();
57         dt.TableName = typeof(T).Name;
58 
59         PropertyInfo[] props=t.GetType().GetProperties();
60         foreach (PropertyInfo p in props)
61         {
62             string propName = p.Name;
63             Type propType=p.PropertyType;
64 
65             if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
66             {
67                 propType = p.PropertyType.GetGenericArguments()[0];
68             }
69             dt.Columns.Add(propName,propType);
70         }
71 
72         //赋值
73         foreach (var item in list)
74         {
75             DataRow row = dt.NewRow();
76             foreach (PropertyInfo p in props)
77             { 
78                 var propValue=p.GetValue(item,null);
79                 row[p.Name] = propValue;
80             }
81             dt.Rows.Add(row);
82         }
83 
84         return dt;
85     }
86 }

 

转载于:https://www.cnblogs.com/glory0727/p/8973981.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值