下面这个学习,把List<T>转换为Datatable。

下面先创建一个对象T:

class Ay{private int _ID;public int ID{get { return _ID; }set { _ID = value; }}private string _Account;public string Account{get { return _Account; }set { _Account = value; }}private string _Email;public string Email{get { return _Email; }set { _Email = value; }}public Ay(){}public Ay(int id,string account,string email){this._ID = id;this._Account = account;this._Email = email;}}

Source Code

此时,你可以写一个C#扩展方法:

public static DataTable ToDataTable<T>(this List<T> items){DataTable dataTable = new DataTable();PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);foreach (PropertyInfo prop in Props){                dataTable.Columns.Add(prop.Name);}foreach (T obj in items){var values = new object[Props.Length];for (int i = 0; i < Props.Length; i++){values[i] = Props[i].GetValue(obj, null);}dataTable.Rows.Add(values);}return dataTable;}

Source Code

下面,我们在List<T>添加几笔记录,然后使用上面的扩展方法,把List<T> 转换为DataTable.
为了看到结果,再把DataTable打印至控制台上:

class Az{public void ConvertListToDataTableTextDemo(){List<Ay> ays = new List<Ay>(){new Ay() { ID=1,Account="A0001",Email="A0001@email.com" },new Ay() { ID=2,Account="A0002",Email="A0002@email.com"},new Ay() {ID=3,Account="A0003",Email="A0003@email.com" }};DataTable dt =  ays.ToDataTable();foreach (DataRow row in dt.Rows){Console.WriteLine();for (int x = 0; x < dt.Columns.Count; x++){Console.Write(row[x].ToString() + " ");}}Console.WriteLine("\n\r");}}

Source Code

控制台输出的结果:

转载于:https://www.cnblogs.com/insus/p/8043173.html

把ListT转换为DataTable相关推荐

  1. ListT转换为DataTable

    关于List<T>转换为DataTable的问题一直是开发过程中经常用到的,现从网上整理几个常用的转换方式,留作备用. (尊重作者原创,本文代码转发自 List 与 DataTable 转 ...

  2. C#将LINQ数据集转换为Datatable

    C#将LINQ数据集转换为Datatable 1.方法一:(测试可用) //通过一个公共类将LINQ数据集转换为datatable public DataTable LINQToDataTable&l ...

  3. 将DataRow转换为DataTable

    /// <summary>/// DataRow转换为DataTable/// </summary>/// <param name="dt">& ...

  4. C# 数组转换为DataTable 的三个方法

    using System; using System.Data; namespace ArrayToDataTable { class ArrayToDataTable { /// <summa ...

  5. SqlDataReader转换为DataTable

    SqlDataReader转换为DataTable 作者:未知 时间: 2007-6-11 13:05:56 文档类型:未知 来自:未知 浏览统计:37   代码一:    private void  ...

  6. 【c#操作office】--OleDbDataAdapter 与OleDbDataReader方式读取excel,并转换为datatable

     OleDbDataAdapter方式: /// <summary>/// 读取excel的表格放到DataTable中 ---OleDbDataAdapter /// </su ...

  7. OleDbDataAdapter与OleDbDataReader方式读取EXCEL并转换为DataTable

    OleDbDataAdapter方式: /// <summary>/// 读取excel的表格放到DataTable中 ---OleDbDataAdapter /// </summa ...

  8. .Net 中HashTable,HashMap 和 Dictionarykey,value 和ListT和DataTable的比较

    转载自http://www.cnblogs.com/jilodream/p/4219840.html (一)HashTable    和Dic    数据结构 Hashtable和Dictionary ...

  9. C# DataRow数组转换为DataTable

    public DataTable ToDataTable(DataRow[] rows) { if (rows == null || rows.Length == 0) return null; Da ...

最新文章

  1. 洛谷P3966 [TJOI2013]单词(后缀自动机)
  2. C#的委托事件在winform窗体中实现传值备忘
  3. mybatis转义反斜杠_Shell echo命令:输出字符串
  4. 简单了解RestTemplate消息读取的转化
  5. 记录几款比较好用的jquery插件
  6. Spring Boot教程(11) – 理解注解@ControllerAdvice
  7. 错误An entity with the same identity already exists in this EntitySet RIA
  8. 一步步学习微软InfoPath2010和SP2010--第九章节--使用SharePoint用户配置文件Web service(2)--在事件注册表单上创建表单加载规则...
  9. cmap参数 plt_plt.imshow的参数有哪些?
  10. 索尼MOTO等压榨国内代工厂:员工宿舍像监狱
  11. OpenSUSE11实现pam_mysql对FTP认证管理
  12. 什么叫直播秒开?如何实现秒开?
  13. 如何在矩池云GPU云中安装MATLAB 2019b软件
  14. 经典Mathematica函数大全
  15. JMeter功能不够用?看我的
  16. 人脸检测(一)--综述
  17. Web浏览器发送POST请求
  18. iphone导出视频 无法连接到设备_爱思助手无法识别设备或者连接超时解决办法...
  19. 计算n阶行列式的C语言实现
  20. ORAN C平面 Section Type 3

热门文章

  1. Python 标准库之 subprocesss
  2. 时间处理_pandas_时间处理小结
  3. LeetCode简单题之将整数转换为两个无零整数的和
  4. OpenCL通用异构开放环境
  5. 双精度张量内核加快了高性能计算
  6. YOLOv4:目标检测(windows和Linux下Darknet 版本)实施
  7. 2021年大数据常用语言Scala(三十四):scala高级用法 异常处理
  8. [JAVA EE]Spring Boot 控制层:参数传递方法
  9. Python数据挖掘:数据探索,数据清洗,异常值处理
  10. Recyclerview 添加一个数组