list转换成DataTable
list转换成DataTable类如下:
public static DataTable ToDataTable<T>(this IList<T> datas)
{
DataTable dt = new DataTable();
Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();
List<KeyValuePair<PropertyInfo, ExportFieldAttribute>> propertyInfos = new List<KeyValuePair<PropertyInfo, ExportFieldAttribute>>(); //获取需要导出列信息
foreach (PropertyInfo propertyInfo in properties)
{
ExportFieldAttribute attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(ExportFieldAttribute)) as ExportFieldAttribute;
if (attribute != null)
{
propertyInfos.Add(new KeyValuePair<PropertyInfo, ExportFieldAttribute>(propertyInfo, attribute));
}
} propertyInfos= propertyInfos.OrderBy(p => p.Value.Sort).ToList();//列排序 //添加列
propertyInfos.ForEach(p =>
{
DataColumn dataColumn = new DataColumn(p.Key.Name, p.Key.PropertyType);
dt.Columns.Add(dataColumn);
} ); datas.ToList().ForEach(data =>
{
DataRow dtRow = dt.NewRow();
foreach (PropertyInfo propertyInfo in propertyInfos.Select(p => p.Key))
{
dtRow[propertyInfo.Name] = propertyInfo.GetValue(data, null);
}
dt.Rows.Add(dtRow);
}
);
////填充数据
//foreach (T data in datas)
//{
// DataRow dtRow = dt.NewRow();
// foreach (PropertyInfo propertyInfo in propertyInfos.Select(p=>p.Key))
// {
// dtRow[propertyInfo.Name]=propertyInfo.GetValue(data, null);
// }
// dt.Rows.Add(dtRow);
//} return dt;
}
属性类如下:
public class ExportFieldAttribute : Attribute
{
public ExportFieldAttribute()
{
} public ExportFieldAttribute(int sort)
{
Sort = sort;
}
public int Sort { get; set; }//排序
}
属性标记的类如下:
public class DiscardStatics
{
private string _sn; [ExportFieldAttribute(Sort=)]
public string Sn
{
get { return _sn; }
set { _sn = value; }
} private string _propertyName; [ExportFieldAttribute()]
public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
private string aa { get; set; } private string _type;
[ExportFieldAttribute()]
public string Type
{
get { return _type; }
set { _type = value; }
} private DateTime _discardTime;
[ExportFieldAttribute()]
public DateTime DiscardTime
{
get { return _discardTime; }
set { _discardTime = value; }
} private double residuals;
[ExportFieldAttribute()]
public double Residuals
{
get { return residuals; }
set { residuals = value; }
} private string _propertyId;
public string PropertyId
{
get { return _propertyId; }
set { _propertyId = value; }
} private string _brand;
public string Brand
{
get { return _brand; }
set { _brand = value; }
} private string _supplier;
public string Supplier
{
get { return _supplier; }
set { _supplier = value; }
} private string _contactInfo;
public string ContactInfo
{
get { return _contactInfo; }
set { _contactInfo = value; }
} private string _childPtyId;
public string ChildPtyId
{
get { return _childPtyId; }
set { _childPtyId = value; }
} private string _childPtyName;
public string ChildPtyName
{
get { return _childPtyName; }
set { _childPtyName = value; }
}
}
list转换成DataTable的更多相关文章
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...
- C#_List转换成DataTable
/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list" ...
- 将List转换成DataTable
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据
领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其 ...
- C# DataTable转换成实体列表 与 实体列表转换成DataTable
/// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T"&g ...
- 获取报告 Stream转string,利用字符串分割转换成DataTable
protected void Button1_Click(object sender, EventArgs e) { MemoryStream stream = new MemoryStream(); ...
- C#:CsvReader读取.CSV文件并转换成DataTable
原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...
- 将泛类型集合List类转换成DataTable
/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...
- C#:CsvReader读取.CSV文件(转换成DataTable)
原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...
随机推荐
- maven中解决javax.servlet.jsp.PageContext cannot be resolved to a type
在eclipse环境下用maven出现:javax.servlet.jsp.PageContext cannot be resolved to a type. 这是由于没有引入jsp-api引发的问题 ...
- jquery,extjs中的extend用法小结
在jquery中,extend其实在做插件时还是用的比较多的,今天同时小结jquery和ext js中 的extend用法,先来看jquery中的. 1) extend(dest,src1,src2 ...
- k-means聚类JAVA实例
<mahout in action>第六章. datafile/cluster/simple_k-means.txt数据集例如以下: 1 1 2 1 1 2 2 2 3 3 8 8 8 9 ...
- Android 实现自动接听和挂断电话功能
添加权限 <uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permis ...
- 关于对defer的理解.
代码 <script defer> function init(){ document.getElementById("div").innerHTML="OK ...
- Chrome 开发者工具的Timeline和Profiles提高Web应用程序的性能
Chrome 开发者工具的Timeline和Profiles提高Web应用程序的性能 二.减少 HTTP 的请求数 当用户浏览页面时,如果我们在用户第一次访问时将一些信息一次性加载到客户端缓存, ...
- 通过Javascript模拟登陆Windows认证的网站
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>模拟登陆</title ...
- 浅析SkipList跳跃表原理及代码实现
本文将总结一种数据结构:跳跃表.前半部分跳跃表性质和操作的介绍直接摘自<让算法的效率跳起来--浅谈“跳跃表”的相关操作及其应用>上海市华东师范大学第二附属中学 魏冉.之后将附上跳跃表的源代 ...
- 基于jQuery的图片相册滑出放大插件
今天给大家带来一款基于jQuery的图片相册滑出放大插件.点击相册图片,展示该图片.该插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗..效 ...
- Linux编程之《只运行一个实例》
概述 有些时候,我们要求一个程序在系统中只能启动一个实例.比如,Windows自带的播放软件Windows Medea Player在Windows里就只能启动一个实例.原因很简单,如果同时启动几个实 ...