C#将List<T>转化为DataTable
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text; namespace ConsoleApplication1
{
static class ConvertDatatable
{
/// <summary>
/// 扩展方法:将List<T>转化为DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this List<T> list)
{
DataTable datatable = new DataTable();
PropertyInfo[] propInfo = typeof(T).GetProperties(BindingFlags.Public|BindingFlags.Instance);
foreach (var item in propInfo)
{
datatable.Columns.Add(item.Name);
}
foreach (T item in list)
{
var values=new object[propInfo.Length];
for (int i = ; i < propInfo.Length; i++)
{
values[i] = propInfo[i].GetValue(item, null);
}
datatable.Rows.Add(values);
}
return datatable;
}
} class Student
{
public int? id { get; set; }
public string name { get; set; }
public int? age { get; set; }
public string address { get; set; }
}
}
调用:
class Program
{
static void Main(string[] args)
{
List<Student> listStu = new List<Student>()
{
new Student(){id=,name="张三",age=,address="东十路1号"},
new Student(){id=,name="李四",age=}
}; DataTable dt = listStu.ToDataTable<Student>(); foreach (DataRow item in dt.Rows)
{
Console.Write(item["id"].ToString());
Console.Write(item["name"].ToString());
Console.Write(item["age"].ToString());
Console.Write(item["address"].ToString());
Console.WriteLine("\r\n");
}
Console.Read();
}
}
C#将List<T>转化为DataTable的更多相关文章
- C# 将list<>泛型集合 转化为 DataTable
使用案例:将页面easy ui 中datagrid表格中的数据,存成json字符串, 通过ajax和ashx传入C#将string类型的json字符串解析成list<>泛型集合, 由于业务 ...
- 泛型集合转化为DataTable
public class DataTableUtil { /// <summary> /// 泛型集合转化为dataTable /// </summary> /// <t ...
- 怎么使用Aspose.Cells读取excel 转化为Datatable
说明:vs2012 asp.net mvc4 c# 使用Aspose.Cells 读取Excel 转化为Datatable 1.HTML前端代码 <%@ Page Language=" ...
- Xml转化为DataTable
/// <summary> /// XML转换为DataTable /// </summary> /// <param name="fileName" ...
- DataSet转化为DataTable
. DataTable dt = ds.Tables[]; . DataTable dt = dao.FillTables("GetOptions_DKI_City_HCPName" ...
- c# 多维数组、交错数组(转化为DataTable)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- MVC 之集合类转化为DataTable
private static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); if (list.C ...
- 使用WCF传输DataTable:DataTable和Xml格式的字符串相互转换(C#)
背景:项目中要用到客户端向服务端传数据,使用WCF,绑定webHttpBinding,做了一个小例子. 业务逻辑简介:客户端在a表中添加了几条数据,从SQL Server数据库直接取出新添加的数据(D ...
- WPF中Grid绑定DataTable数据。
1.首先引用DocumentFormat.OpenXml.dll 2.然后新建一个OpenExcelHelper类,将Excel转化为Datatable. /// <summary> ...
随机推荐
- linux安装nginx,遇坑解决
1.nginx官网下载tar包,解压linux下: 2.进入解压文件夹,执行./configure: 3.报错,原因没有安装nginx相关依赖,如gcc环境,PCRE依赖库 ,zlib 依赖库 ,Op ...
- 在react/redux中使用Immutable
在redux中使用Immutable 1.什么是Immutable? Immutable是一旦创建,就不能被更改的数据. 对Immutable对象的任何修改或添加删除操作都会返回一个新的Immutab ...
- 搜狗浏览器或者360浏览器安装chrome 浏览器插件
https://www.cnblogs.com/ingvar/p/6403777.html#undefined
- case语法2
case流程控制语句在linux中有其独到的一面,使得在编程过程中能够在脚本编写的过程中具备多个选项功能,使其功能多样化,其具备简单,快速的特点. 一.case流程语句结构图 根据流程图可以知道,在所 ...
- 一十九条优雅Python编程技巧
1.交换赋值 #不推荐 temp = a a = b b = a #推荐 a , b = b , a #先生成一个元组(tuple)对象,然后在unpack 2.Unpacking #不推荐 l = ...
- JS的小判断
// 0 if(undefined) { console.log('1'); } else { console.log('0'); } // 0 if(null) { console.log('1') ...
- ecmall 移动端 微信分享
/* 用户判断是否在微信端 */ $this->assign('isWeixin', isWeixin()); //isWeixin() 在系统核心基础类的ecmall.php里定义好了 是微信 ...
- Python之DataFrame常用方法小结
https://blog.csdn.net/a786150017/article/details/78573055
- Dubbo透传traceId/logid的一种思路
前言: 随着dubbo的开源, 以及成为apache顶级项目. dubbo越来越受到国内java developer欢迎, 甚至成为服务化自治的首选方案. 随着微服务的流行, 如何跟踪整个调用链, 成 ...
- Linux 挂载windows目录
1.默认情况下,Linux服务器会装有samba-client,但是没有装samba-server.但是访问Windows系统共享,安装有samba-client就可以了. [root@test ~] ...