闲来无事,只有写代码啦,以下为DataTable转List与List转DataTable的两个方法,主要技术点用到了反射原理:

  1. /// <summary>
  2. /// 模型转换类
  3. /// </summary>
  4. public class ConvertModel
  5. {
  6. /// <summary>
  7. /// DataTable转List
  8. /// </summary>
  9. /// <typeparam name="T">list中的类型</typeparam>
  10. /// <param name="dt">要转换的DataTable</param>
  11. /// <returns></returns>
  12. public static List<T> DatatTableToList<T>(DataTable dt) where T : class, new()
  13. {
  14. List<T> list = new List<T>();
  15. T t = new T();
  16. PropertyInfo[] prop = t.GetType().GetProperties();
  17. //遍历所有DataTable的行
  18. foreach (DataRow dr in dt.Rows)
  19. {
  20. t = new T();
  21. //通过反射获取T类型的所有成员
  22. foreach (PropertyInfo pi in prop)
  23. {
  24. //DataTable列名=属性名
  25. if (dt.Columns.Contains(pi.Name))
  26. {
  27. //属性值不为空
  28. if (dr[pi.Name] != DBNull.Value)
  29. {
  30. object value = Convert.ChangeType(dr[pi.Name], pi.PropertyType);
  31. //给T类型字段赋值
  32. pi.SetValue(t, value, null);
  33. }
  34. }
  35. }
  36. //将T类型添加到集合list
  37. list.Add(t);
  38. }
  39. return list;
  40.  
  41. }
  42.  
  43. /// <summary>
  44. /// List转换为DataTable
  45. /// </summary>
  46. /// <typeparam name="T">List中的类型</typeparam>
  47. /// <param name="list">要转换的list</param>
  48. /// <returns></returns>
  49. public static DataTable ListToDataTable<T>(List<T> list) where T : class
  50. {
  51. DataTable dt = new DataTable();
  52. PropertyInfo[] prop = typeof(T).GetProperties();
  53. DataColumn[] ColumnArr = prop.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray();
  54. dt.Columns.AddRange(ColumnArr);
  55. foreach (T t in list)
  56. {
  57. DataRow dr = dt.NewRow();
  58. foreach (PropertyInfo pi in prop)
  59. {
  60. if (dt.Columns.Contains(pi.Name))
  61. {
  62. if (pi.GetValue(t) != null)
  63. {
  64. dr[pi.Name] = pi.GetValue(t);
  65. }
  66. }
  67. }
  68. dt.Rows.Add(dr);
  69. }
  70.  
  71. return dt;
  72. }
  73. }

调用:

  1. DataTable dt = new DataTable();
  2. dt.Columns.Add("Id");
  3. dt.Columns.Add("Sex");
  4. dt.Columns.Add("Age");
  5. dt.Columns.Add("Height");
  6. DataRow dr = dt.NewRow();
  7. dr["Id"] = ;
  8. dr["Sex"] = ;
  9. dr["Age"] = ;
  10. dr["Height"] = ;
  11. dt.Rows.Add(dr);
  12. //将DataTable转换为List<Persion>
  13. List<Persion> list1 = ConvertModel.DatatTableToList<Persion>(dt);
  14.  
  15. List<Persion> list = new List<Persion>()
  16. {
  17. new Persion(){Id=,Sex=,Age=,Height=},
  18. new Persion(){Id=,Sex=,Age=,Height=},
  19. };
  20. //将List<Persion>转换为DataTable
  21. DataTable dt1 = ConvertModel.ListToDataTable<Persion>(list);

C#之DataTable转List与List转Datatable的更多相关文章

  1. C# DataTable转List And List转DataTable

    // DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...

  2. “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用

    “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用 造成这个错误的原因是,在 ...

  3. 多个不同的表合并到一个datatable中,repeater在绑定datatable

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  4. 将两个列不同的DataTable合并成一个新的DataTable

    /// <summary>         /// 将两个列不同(结构不同)的DataTable合并成一个新的DataTable         /// </summary> ...

  5. C#给DataTable添加序号、C#给DataTable添加合计、小计

    /// <summary>        /// 给DataTable添加序号        /// </summary>        /// <param name= ...

  6. Datatable的查找和排序(Datatable.Select)

    Datatable  是一种常用的数据结构.数据类型有点类似于数据库中的表结构.在没有使用优秀的orm框架前,大部分的数据库的数据都是先变为Datatable 然后再通过代码转换变成 object. ...

  7. EasyUI - Datatable转Json and Json转Datatable

    using System; using System.Data; using System.Linq; using System.Collections; using System.Collectio ...

  8. C# 将DataTable一行放入另一个DataTable中

    http://blog.csdn.net/huyu107/article/details/53509171 概述 从一个DataTable中取一行放到另一个DataTable里报错: 该行已经属于另一 ...

  9. NPOI json转Excel DataTable转Excel ,Excel转DataTable

    JsonToExcel: public static void JsonToExcel(List<Dictionary<string, object>> json, strin ...

随机推荐

  1. 修复Java使用POI合并Excel单元格后,边框不显示的问题

    使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决. 创建方法: public void setBorderStyle(int bor ...

  2. 从头开始基于Maven搭建SpringMVC+Mybatis项目(1)

    技术发展日新月异,许多曾经拥有霸主地位的流行技术短短几年间已被新兴技术所取代. 在Java的世界中,框架之争可能比语言本身的改变更让人关注.近几年,SpringMVC凭借简单轻便.开发效率高.与spr ...

  3. TCP/IP(六)应用层(DNS和HTTP协议)

    前言 到这一篇我已经把TCP/IP五层模型详细的说明了一遍,大体的从物理层到最上层的应用层做了一个大概的了解,其实总体学下来东西非常的多,我们需要经常的去系统性的去学习它.不然过一段时间就忘记了! 回 ...

  4. CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A-Who is the winner?

    A. Who is the winner? time limit per test 1 second memory limit per test 64 megabytes input standard ...

  5. 2017ecjtu-summer training #4 CodeForces 731C

    C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  6. 最长上升子序列(LIS经典变型) dp学习~5

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...

  7. DFS(dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2212 DFS Time Limit: 5000/2000 MS (Java/Others)    Me ...

  8. 基于Vue的页面切换左右滑动效果

    HTML文本页面: <template> <div id="app> <transition :name="direction" mode= ...

  9. Java Reflection(getXXX和getDeclaredXXX)

    package com.sunchao.reflection; public class Person { private int age ; private String name; public ...

  10. 百度Apollo 尝试

    从Git-Hub上下载了Apollo源码在Ubuntu上准备运行一下 完成了以下步骤: bash docker/scripts/install_docker.sh bash docker/script ...