public class List2DataTable     {

public static string GetClassName(Type type)

{             if (type == null)

throw new ArgumentException("参数type不能为空");

if (type.HasAttribute<AliasAttribute>())

return type.GetAttribute<AliasAttribute>().Name.ToLower();

else

return type.Name.ToLower();

}

public static DataTable ListToDataTable<T>(List<T> oList) where T : class

{

try

{

string tableName = GetClassName(typeof(T));

DataTable dt = new DataTable();

string fieldName;

object fieldValue;

string[] fieldNames;

System.Reflection.PropertyInfo[] Props = typeof(T).GetProperties();

fieldNames = new string[Props.Length];

for (int i = 0; i < Props.Length; i++)

{

fieldName = Props[i].Name;

fieldNames[i] = fieldName;

dt.Columns.Add(fieldName, Props[i].PropertyType);

}

for (int r = 0; r < oList.Count; r++)

{

DataRow dr = dt.NewRow();

T o = oList[r];

for (int i = 0; i < fieldNames.Length; i++)

{

fieldValue = Props[i].GetValue(o, null);

dr[fieldNames[i]] = fieldValue;

}

dt.Rows.Add(dr);

}

return dt;

}

catch(Exception ex)             {             }

return null;

}

}

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

  1. dataGrid转换dataTable

    #region dataGrid转换dataTable   /// <summary>   /// dataGrid转换dataTable   /// </summary>   ...

  2. C# 将Excel以文件流转换DataTable

    /* *引用 NuGet包 Spire.XLS */ /// <summary> /// Excel帮助类 /// </summary> public class ExcelH ...

  3. List转换DataTable

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  4. DataRow数组转换DataTable

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

  5. C# DataRow[]转换DataTable

    DataTable dt = ... DataRow[] dr = dt.Select("ID=14"); dt = dr.CopyToDataTable();

  6. c# 将csv文件转换datatable的两种方式。

    第一种: public static DataTable csvdatatable(string path) { DataTable dt = new DataTable(); string conn ...

  7. 实体lis<T>t转换datatable

    public static DataTable ListToTable<T>(List<T> list) {             Type type = typeof(T) ...

  8. 读CSV转换datatable

    using System.Data; using System.IO;   /// <summary> /// Stream读取.csv文件 /// </summary> // ...

  9. SqL读取XML、解析XML、SqL将XML转换DataTable、SqL将XML转换表

    DECLARE @ItemMessage XML )) SET @ItemMessage=N' <ReceivablesInfos> <ReceivablesList> < ...

随机推荐

  1. Windows平台下安装Hadoop

    今天参照这个网址(http://www.cnblogs.com/kinglau/archive/2013/08/20/3270160.html)安装了下,前面七步没有问题. 到第八步出问题了,后来看了 ...

  2. 使用 Spring 3 来创建 RESTful Web Services

    来源于:https://www.ibm.com/developerworks/cn/web/wa-spring3webserv/ 在 Java™ 中,您可以使用以下几种方法来创建 RESTful We ...

  3. Web.config自定义节点configSections

    1.为什么需要自定义节点 为了增加应用程序的可移植性,通常网站需要配置一些自定义的节点,例如:文件上传的路径等,再深入的应用,可以定义工厂方法需要创建的类. 2.configSections使用方法 ...

  4. iOS开发中的错误整理,IOS9中canOpenURL调用失败分析

    由于IOS加入对用户隐私以及禁止扫描系统信息的控制,目前通过canOpenURL的方法来判断用户是否安装特定app,则会出现-canOpenURL: failed for URL: "ABC ...

  5. 【BZOJ 1877】【SDOI 2009】晨跑

    拆点跑$MCMF最小费用最大流$ 复习一下$MCMF$模板啦啦啦--- 一些坑:更新$dist$后要接着更新$pre$,不要判断是否在队列中再更新,,,听不懂吧,听不懂就对了,因为只有我才会在这种错误 ...

  6. MyEclipse删除不再使用的工作空间记录

    找到安装目录下的configuration/.settings/org.eclipse.ui.ide.prefs文件,打开此文件,删除不再使用的工作空间信息,重启MyEclipse.然后在切换工作空间 ...

  7. DTD中的属性类型

    <![CDATA[文本内容]]> DTD中的属性类型 全名:character data 在标记CDATA下,所有的标记.实体引用都被忽略,而被XML处理程序一视同仁地当做字符数据看待, ...

  8. Maven插件maven-surefire-plugin

    插件官方文档:maven-surefire-plugin 1.surefire plugin的作用surefire 插件用来在maven构建生命周期的test phase执行一个应用的单元测试. 它会 ...

  9. Java追加文件内容的三种方法

    import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...

  10. Win7另存文件没有桌面的解决方法

    今日一朋友保存文件随口说了一句我那个桌面找不到了...我略感惊奇,没遇到这么个情况.决定,问度娘,在此做下记录,以便以后自己或朋友查阅. 我们在网上另存一个文件,打开另存窗口,发现没有“桌面” 在收藏 ...