C#之DataTable转List与List转Datatable
闲来无事,只有写代码啦,以下为DataTable转List与List转DataTable的两个方法,主要技术点用到了反射原理:
- /// <summary>
- /// 模型转换类
- /// </summary>
- public class ConvertModel
- {
- /// <summary>
- /// DataTable转List
- /// </summary>
- /// <typeparam name="T">list中的类型</typeparam>
- /// <param name="dt">要转换的DataTable</param>
- /// <returns></returns>
- public static List<T> DatatTableToList<T>(DataTable dt) where T : class, new()
- {
- List<T> list = new List<T>();
- T t = new T();
- PropertyInfo[] prop = t.GetType().GetProperties();
- //遍历所有DataTable的行
- foreach (DataRow dr in dt.Rows)
- {
- t = new T();
- //通过反射获取T类型的所有成员
- foreach (PropertyInfo pi in prop)
- {
- //DataTable列名=属性名
- if (dt.Columns.Contains(pi.Name))
- {
- //属性值不为空
- if (dr[pi.Name] != DBNull.Value)
- {
- object value = Convert.ChangeType(dr[pi.Name], pi.PropertyType);
- //给T类型字段赋值
- pi.SetValue(t, value, null);
- }
- }
- }
- //将T类型添加到集合list
- list.Add(t);
- }
- return list;
- }
- /// <summary>
- /// List转换为DataTable
- /// </summary>
- /// <typeparam name="T">List中的类型</typeparam>
- /// <param name="list">要转换的list</param>
- /// <returns></returns>
- public static DataTable ListToDataTable<T>(List<T> list) where T : class
- {
- DataTable dt = new DataTable();
- PropertyInfo[] prop = typeof(T).GetProperties();
- DataColumn[] ColumnArr = prop.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray();
- dt.Columns.AddRange(ColumnArr);
- foreach (T t in list)
- {
- DataRow dr = dt.NewRow();
- foreach (PropertyInfo pi in prop)
- {
- if (dt.Columns.Contains(pi.Name))
- {
- if (pi.GetValue(t) != null)
- {
- dr[pi.Name] = pi.GetValue(t);
- }
- }
- }
- dt.Rows.Add(dr);
- }
- return dt;
- }
- }
调用:
- DataTable dt = new DataTable();
- dt.Columns.Add("Id");
- dt.Columns.Add("Sex");
- dt.Columns.Add("Age");
- dt.Columns.Add("Height");
- DataRow dr = dt.NewRow();
- dr["Id"] = ;
- dr["Sex"] = ;
- dr["Age"] = ;
- dr["Height"] = ;
- dt.Rows.Add(dr);
- //将DataTable转换为List<Persion>
- List<Persion> list1 = ConvertModel.DatatTableToList<Persion>(dt);
- List<Persion> list = new List<Persion>()
- {
- new Persion(){Id=,Sex=,Age=,Height=},
- new Persion(){Id=,Sex=,Age=,Height=},
- };
- //将List<Persion>转换为DataTable
- DataTable dt1 = ConvertModel.ListToDataTable<Persion>(list);
C#之DataTable转List与List转Datatable的更多相关文章
- C# DataTable转List And List转DataTable
// DataTable转List: IList<HousesEntity> Ilist = TableAndList.ConvertTo<HousesEntity>(dt); ...
- “DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用
“DataTable”是“System.Data.DataTable”和“Microsoft.Office.Interop.Excel.DataTable”之间的不明确的引用 造成这个错误的原因是,在 ...
- 多个不同的表合并到一个datatable中,repeater在绑定datatable
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- 将两个列不同的DataTable合并成一个新的DataTable
/// <summary> /// 将两个列不同(结构不同)的DataTable合并成一个新的DataTable /// </summary> ...
- C#给DataTable添加序号、C#给DataTable添加合计、小计
/// <summary> /// 给DataTable添加序号 /// </summary> /// <param name= ...
- Datatable的查找和排序(Datatable.Select)
Datatable 是一种常用的数据结构.数据类型有点类似于数据库中的表结构.在没有使用优秀的orm框架前,大部分的数据库的数据都是先变为Datatable 然后再通过代码转换变成 object. ...
- EasyUI - Datatable转Json and Json转Datatable
using System; using System.Data; using System.Linq; using System.Collections; using System.Collectio ...
- C# 将DataTable一行放入另一个DataTable中
http://blog.csdn.net/huyu107/article/details/53509171 概述 从一个DataTable中取一行放到另一个DataTable里报错: 该行已经属于另一 ...
- NPOI json转Excel DataTable转Excel ,Excel转DataTable
JsonToExcel: public static void JsonToExcel(List<Dictionary<string, object>> json, strin ...
随机推荐
- 修复Java使用POI合并Excel单元格后,边框不显示的问题
使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决. 创建方法: public void setBorderStyle(int bor ...
- 从头开始基于Maven搭建SpringMVC+Mybatis项目(1)
技术发展日新月异,许多曾经拥有霸主地位的流行技术短短几年间已被新兴技术所取代. 在Java的世界中,框架之争可能比语言本身的改变更让人关注.近几年,SpringMVC凭借简单轻便.开发效率高.与spr ...
- TCP/IP(六)应用层(DNS和HTTP协议)
前言 到这一篇我已经把TCP/IP五层模型详细的说明了一遍,大体的从物理层到最上层的应用层做了一个大概的了解,其实总体学下来东西非常的多,我们需要经常的去系统性的去学习它.不然过一段时间就忘记了! 回 ...
- 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 ...
- 2017ecjtu-summer training #4 CodeForces 731C
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 最长上升子序列(LIS经典变型) dp学习~5
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...
- DFS(dfs)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2212 DFS Time Limit: 5000/2000 MS (Java/Others) Me ...
- 基于Vue的页面切换左右滑动效果
HTML文本页面: <template> <div id="app> <transition :name="direction" mode= ...
- Java Reflection(getXXX和getDeclaredXXX)
package com.sunchao.reflection; public class Person { private int age ; private String name; public ...
- 百度Apollo 尝试
从Git-Hub上下载了Apollo源码在Ubuntu上准备运行一下 完成了以下步骤: bash docker/scripts/install_docker.sh bash docker/script ...