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

}

相关推荐
Empty_7771 天前
编程之python基础
开发语言·python
疯狂吧小飞牛1 天前
Lua 中的 __index、__newindex、rawget 与 rawset 介绍
开发语言·junit·lua
寻星探路1 天前
Java EE初阶启程记13---JUC(java.util.concurrent) 的常见类
java·开发语言·java-ee
哲Zheᗜe༘1 天前
了解学习Python编程之python基础
开发语言·python·学习
落日漫游1 天前
数据结构笔试核心考点
java·开发语言·算法
寻找华年的锦瑟1 天前
Qt-配置文件(INI/JSON/XML)
开发语言·qt
HY小海1 天前
【C++】AVL树实现
开发语言·数据结构·c++
workflower1 天前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
Roc-xb1 天前
ModuleNotFoundError: No module named ‘conda_token‘
开发语言·python·conda
人工干智能1 天前
Python 开发中:`.ipynb`(Jupyter Notebook 文件)和 `.py`(Python 脚本文件)
开发语言·python·jupyter