通常情况下,我们在做工程项目的时候,需要把待处理的数据存储在数据库中。 通过 SQL 的 Select 语句很容易把查询的结果以 DataTable 的方式得到,但在对 DateTable 中的数据进行进一步的检索时远远没有模型类 List<T> 方便。 所以,在做工程项目时,会把查询到的 DataTable 转化成模型类 List<T> , 处理完毕后如果有所改动则把这个模型类 换回成 DataTable 以便完成数据库中的相应改动。 上周在做一个电力局项目时对导入的上万条数据进行插入操作就是这样做的,参见一下图文:
技术分析如何实现 DataTable 与 List<T> 的相互转化呢? 这里需要掌握 C# 中的 泛型 以及 反射 的基础知识。 泛型部分: 反射部分: 通过泛型,我们可以把任何一个模型类 List<T> 转化成 DataTable ,也可以把任何一个 DataTable 转化成对应的 List<T> ,只要这里 T 类型的 public 属性 与 DataTable 的 ColumnName 对应即可。当然属性的获取是通过反射技术完成的。 有关泛型和反射的知识,可以查看上面的图文,我在这里就不在重复了。
代码实现下面,通过一个实际项目的例子来说明相互转换的代码与具体的应用。 Step1. 设备台账的结构 AcountItem 。 /// <summary> /// 台账Item /// </summary> public class AccountItem { /// <summary> /// 设备ID /// </summary> public string EquipmentId { get; set; } /// <summary> /// 厂站名称 /// </summary> public string FactoryStationName { get; set; } /// <summary> /// 厂站类型 /// </summary> public string FactoryStationType { get; set; } /// <summary> /// 一次设备名称 /// </summary> public string PrimaryDeviceName { get; set; } /// <summary> /// 一次设备电压等级 /// </summary> public string PrimaryDeviceVoltageLevel { get; set; } /// <summary> /// 制造厂家 /// </summary> public string Manufacturer { get; set; } /// <summary> /// 保护类别 /// </summary> public string ProtectionCategory { get; set; } /// <summary> /// 保护型号 /// </summary> public string ProtectionType { get; set; } /// <summary> /// 软件版本 /// </summary> public string SoftwareVersion { get; set; } /// <summary> /// 保护名称 /// </summary> public string ProtectionName { get; set; } /// <summary> /// 投运日期 /// </summary> public string CommissionDate { get; set; } /// <summary> /// 出厂日期 /// </summary> public string ProductionDate { get; set; } /// <summary> /// 所在屏柜 /// </summary> public string ScreenCabinets { get; set; } /// <summary> /// 保护套别 /// </summary> public string ProtectiveSleeve { get; set; } }
Step2. 把模型类 List<T> 转化为 DataTable 。 public static DataTable ListToDataTable<T>(IEnumerable<T> collection) { if (collection == null) throw new ArgumentNullException();
PropertyInfo[] props = typeof (T).GetProperties(); DataTable dt = new DataTable(); dt.Columns.AddRange(props.Select( p => new DataColumn(p.Name, p.PropertyType) ).ToArray()); if (collection.Any()) { for (int i = 0; i < collection.Count(); i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in props) { object obj = pi.GetValue(collection.ElementAt(i), null); tempList.Add(obj); } object[] array = tempList.ToArray(); dt.LoadDataRow(array, true); } } return dt; }
Step3. 举例,把 List<AcountItem> 转化为 DataTable 。 // 初始化链表并加入数据。 List<AccountItem> lst = new List<AccountItem>();
DataTable dt = ListToDataTable(lst);
dt 数据表列集合的列名依次为:
Step4. DataTable 转化为 List。 public static List<T> DataTableToList<T>(DataTable dt) where T : new() { List<T> result = new List<T>(); foreach (DataRow dr in dt.Rows) { T item = new T(); PropertyInfo[] props = item.GetType().GetProperties(); foreach (PropertyInfo pi in props) { string tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (pi.CanWrite == false) continue;
object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(item, value, null); } } result.Add(item); } return result; }
Step5. 举例,把 Step3 得到的 DataTable 转化为 List<AccountItem> 。 List<AccountItem> lst = DataTableToList(dt);
通过调用 Step4 带约束的泛型方法,可以得到模型类 List<AccountItem> 。
总结到此为止,DataTable 与 List<T> 的相互转换就介绍完了,由于整个项目都是利用 List<T> 来构建逻辑的,所以整个系统可以具有良好的结构,通过 LINQ 也能满足效率的要求。今天就到这里吧!See You!
相关图文:
经过8年多的发展,LSGO软件技术团队在「地理信息系统」、「数据统计分析」、「计算机视觉」等领域积累了丰富的研发经验,也建立了人才培养的完备体系,欢迎对计算机技术感兴趣的同学加入,与我们共同成长进步。 我们图文推送的计划如下,欢迎大家转发! 周一「图书排行:计算机书籍每周销量排行榜」 周二「技术分享:C#语言在工程中的应用」 周三「资料分享:网络上发现的电子资料」 周四「LeetCode实战:算法题目的实现」 周五「猫眼电影:即将上映、最受期待榜」 周六「Github精选:本周10大热门项目」 周日「股市币市:本周交易数据分析与最新公告」
|