DataTable dt = new DataTable();
if (_list != null)
{
//通过反射获取list中的字段
System.Reflection.PropertyInfo[] p = _list[].GetType().GetProperties();
foreach (System.Reflection.PropertyInfo pi in p)
{
dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
}
for (int i = ; i < _list.Count; i++)
{
IList TempList = new ArrayList();
//将IList中的一条记录写入ArrayList
foreach (System.Reflection.PropertyInfo pi in p)
{
object oo = pi.GetValue(_list[i], null);
TempList.Add(oo);
}
object[] itm = new object[p.Length];
for (int j = ; j < TempList.Count; j++)
{
itm.SetValue(TempList[j], j);
}
dt.LoadDataRow(itm, true);
}
}

C#将list转换为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 ...

  10. 把List<T>转换为DataTable

    下面这个学习,把List<T>转换为Datatable. 下面先创建一个对象T: class Ay { private int _ID; public int ID { get { ret ...

随机推荐

  1. 实现IDisposable接口的模式

    代码: public class Class2 : IDisposable { ~Class2() { Dispose(false); } public void Dispose() { Dispos ...

  2. Leetcode#174 Dungeon Game

    原题地址 典型的地图寻路问题 如何计算当前位置最少需要多少体力呢?无非就是在向下走或向右走两个方案里做出选择罢了. 如果向下走,看看当前位置能提供多少体力(如果是恶魔就是负数,如果是草药就是正数),如 ...

  3. 在C语言中嵌入汇编语言

    TMS320C28x编译器允许在C程序中嵌入汇编指令,通过下面声明实现: asm(“assembler text”); 其中assembler text指汇编代码.asm指令一般用来处理C/C++语句 ...

  4. HDU-2604_Queuing

    题目:Problem Description Queues and Priority Queues are data structures which are known to most comput ...

  5. HTTP persistent connection

    http://en.wikipedia.org/wiki/HTTP_persistent_connection

  6. super用法

    Person类: public class Person { String _name; int _age; public Person(String name,int age) { _name= n ...

  7. 分布式数据存储-MySQL主从复制

    前言 一.主从复制过程 MySQL的主从复制能力是通过三个线程来实现的,两个在Slave端的I/O和SQL两个线程,还有一个在Master端I/O线程: Binlog dump thread:Mast ...

  8. ios网站,博客

    中文 网站系列 objcio.cncocoachina.comcode4app.com泰然网 博客系列唐巧地球人都知道哈.http://blog.devtang.com/巧哥新出书了,速度入手吧. 虾 ...

  9. LoaderManager使用详解(二)---了解LoaderManager

    了解LoaderManager   这篇文章将介绍LoaderManager类,这是该系列的第二篇文章. 一:Loaders之前世界 二:了解LoaderManager 三:实现Loaders 四:实 ...

  10. 深入浅出ES6(十三):类 Class

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 你可能觉得之前讲解的内容略显复杂,今天我们就讲解一些相对简单的内容,不再是生成器 ...