c# DataTable 帮助类

public class DataTableHelper

{

#region DataTable转IList

/// <summary>

/// DataTable转IList集合

/// </summary>

/// <typeparam name="T"></typeparam>

/// <param name="dataTable"></param>

/// <returns></returns>

public static IList<T> ToList<T>(DataTable dataTable) where T : class, new()

{

IList<T> list = new List<T>();// 定义集合

if (dataTable != null)

{

foreach (DataRow dr in dataTable.Rows)

{

T t = new T();

PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性

foreach (PropertyInfo pi in propertys)

{

var name = pi.Name;

if (dataTable.Columns.Contains(name))

{

if (!pi.CanWrite) continue;

object value = dr[name];

if (value != DBNull.Value)

{

pi.SetValue(t, value, null);

}

}

}

list.Add(t);

}

}

return list;

}

#endregion

}

相关推荐
埃博拉酱16 小时前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
mudtools18 小时前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的1 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21881 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理1 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi1 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1231 天前
matlab画图工具
开发语言·matlab
加号31 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
dustcell.1 天前
haproxy七层代理
java·开发语言·前端
norlan_jame1 天前
C-PHY与D-PHY差异
c语言·开发语言