1. /// <summary>
  2. /// 将泛类型集合List类转换成DataTable
  3. /// </summary>
  4. /// <param name="list">泛类型集合</param>
  5. /// <returns></returns>
  6. public static DataTable ListToDataTable<T>(List<T> entitys)
  7. {
  8. //检查实体集合不能为空
  9. if (entitys == null || entitys.Count < 1)
  10. {
  11. throw new Exception("需转换的集合为空");
  12. }
  13. //取出第一个实体的所有Propertie
  14. Type entityType = entitys[0].GetType();
  15. PropertyInfo[] entityProperties = entityType.GetProperties();
  16.  
  17. //生成DataTable的structure
  18. //生产代码中,应将生成的DataTable结构Cache起来,此处略
  19. DataTable dt = new DataTable();
  20. for (int i = 0; i < entityProperties.Length; i++)
  21. {
  22. //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType);
  23. dt.Columns.Add(entityProperties[i].Name);
  24. }
  25. //将所有entity添加到DataTable中
  26. foreach (object entity in entitys)
  27. {
  28. //检查所有的的实体都为同一类型
  29. if (entity.GetType() != entityType)
  30. {
  31. throw new Exception("要转换的集合元素类型不一致");
  32. }
  33. object[] entityValues = new object[entityProperties.Length];
  34. for (int i = 0; i < entityProperties.Length; i++)
  35. {
  36. entityValues[i] = entityProperties[i].GetValue(entity, null);
  37. }
  38. dt.Rows.Add(entityValues);
  39. }
  40. return dt;
  41. }

  

List转换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<T> 转换 DataTable

    public class List2DataTable     { public static string GetClassName(Type type) {             if (typ ...

  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. 单点登录(SSO)系统的总结

    前些天一位其他开发部门的同事找到我们了解一些关于SSO单点登录的事,他们要做单点登录,同时也需要和我们这边的系统做集成,要我帮忙做一单点登录,了解关于单点登录的解决方案和资料,虽然做单点登录已经很久了 ...

  2. php常用时间戳记录

    <?php echo '<br/>'; //php获取今日开始时间戳和结束时间戳 echo "今天"; echo '<br/>'; $beginTod ...

  3. python 选择排序

    选择排序算法的思想,首先第一次先从整个序列中选择最小的数,然后放到第一位,然后再从第二位到最后一位选择出最小的一个数,把这个数放到第二位,然后,再从第三位到最后一位选择其中最小的数放到第三位,这样一直 ...

  4. Nginx for Windows 使用笔记

    一.常见启动错误 1. "No mapping for the Unicode character exists in the target multi-byte code page&quo ...

  5. Junit使用

    eclipse Junit的简单使用: eclipse自带了Junit插件. 安装Junit,如下图所示,右键项目-->Properties-->Add Library 选择Junit 选 ...

  6. SQl语句学习笔记(二)

    merge into        when matched then...  when not mached then... merge into t_road_pre_parameter a fr ...

  7. solr6.1-----mysql 数据导入-查询

    此部分一定要细心,lz 中间错了一个细节,调了好长时间(汗).请严格按照步骤操作 新建core 步骤1: 在webapps中solrhome下新建一个文件夹名字叫做collection1(名字不固定, ...

  8. Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...

  9. Linux 中常见的命令行,持续更新

    1.添加自己的环境变量 root@adonis:~# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin r ...

  10. jquery隐藏按钮

    $(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...