下面这个学习,把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 = ; 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=,Account="A0001",Email="A0001@email.com" },
new Ay() { ID=,Account="A0002",Email="A0002@email.com"},
new Ay() {ID=,Account="A0003",Email="A0003@email.com" }
}; DataTable dt = ays.ToDataTable(); foreach (DataRow row in dt.Rows)
{
Console.WriteLine();
for (int x = ; x < dt.Columns.Count; x++)
{
Console.Write(row[x].ToString() + " ");
}
} Console.WriteLine("\n\r"); }
}

Source Code

控制台输出的结果:

把List<T>转换为DataTable的更多相关文章

  1. 对象列表转换为DataTable或DataTable转换为对象列表.

    /**********************************************************************************/ // 说明: 数据转换工具. ...

  2. linq之将IEnumerable<T>类型的集合转换为DataTable类型 (转载)

    在考虑将表格数据导出成excel的时候,网上搜的时候找到一个特别合适的公共方法,可以将query查询出来的集合转换为datatable 需引用using System.Reflection; publ ...

  3. Json 字符串 转换为 DataTable数据集合

    /// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...

  4. c#常用的Datable转换为json,以及json转换为DataTable操作方法

    #region  DataTable 转换为Json字符串实例方法 /// <summary> /// GetClassTypeJosn 的摘要说明 /// </summary> ...

  5. [工具类]泛型集合转换为DataTable

    写在前面 在实际项目中,用到了将集合转换为DataTable,就试着封装了一个方法,记录一下. 代码 using System; using System.Collections.Generic; u ...

  6. 泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

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

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

  8. C#基础知识之泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  9. Enumerable转换为DataTable

    今天在项目组公共类库中发现一个 Enumerable类型转换为DataTable,写的挺精简的,拿出来跟大家共享一下. using System; using System.Collections.G ...

随机推荐

  1. php向数据库插入数据

    <?php header("Content-type: text/html;charset=utf-8"); $con = mysql_connect("local ...

  2. MySQL主从复制--原理

    简介 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重新执行一 ...

  3. iOS开发NSDate、NSString、时间戳之间的转化

    //将UTCDate(世界标准时间)转化为当地时区的标准Date(钟表显示的时间) //NSDate *date = [NSDate date]; 2018-03-27 06:54:41 +0000 ...

  4. 洗礼灵魂,修炼python(17)--跨平台操作三剑客—os,os.path.sys模块

    os 1.作用: 因为客户基本都是使用不同的操作系统,在不同的系统下,要完成一个项目,那必须跨平台操作,而python本来就是一个跨平台的语言,而有了os模块,则不需要在意什么系统.并且os模块是用于 ...

  5. python编程的简洁代码

    1.列表间元素操作 L1 = [1,3,5,]L2 = [2,5,3,1,8]x = set(L1)y = set(L2)#差集print(y - x)#交集print(y&x)#并集prin ...

  6. C#语言————拼接、插入、替换、删除四种方法

    StringBuilder sb = new StringBuilder("hello"); sb.Append("world");//拼接 sb.Insert ...

  7. MySQL复制ERROR 1794 (HY000): Slave is not configured or failed to initialize properly.

    ERROR 1794 (HY000): Slave is not configured or failed to initialize properly. You must at least set  ...

  8. 高通移植mipi LCD的过程LK代码

    lk部分:(实现LCD兼容) 1. 函数定位 aboot_init()来到target_display_init(): 这就是高通原生lk LCD 兼容的关键所在.至于你需要兼容多少LCD 就在whi ...

  9. February 10th, 2018 Week 6th Saturday

    It is not enough to have a good mind. The main thing is to use it well. 头脑聪明还不够,重要的是好好运用. From Rene ...

  10. January 12th, 2018 Week 02nd Friday

    Nothing behind me, everything ahead of me, as is ever so on the road. 我的身后空空荡荡,整个世界都在前方,这就是在路上. That ...