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; ...
随机推荐
- U8记账凭证修改方法汇总
在输入记账凭证时,尽管账务系统提供了多种控制错误的措施,但错误凭证的出现是难免的,为此,系统必须能够提供对错误凭证修改的功能.目前,许多财 务软件(如:用友.安易.三门)都提供了“反审核.反记账.反结 ...
- window.onload与$.ready的差别
在做图书管理系统的时候.实用到window.onload(){}方法.可是遇到了一个问题.就是怎么都不运行,究竟是为什么呢?愁了半天.后来经师姐指点改用了$.ready(){}. 在我的浅浅的了解中觉 ...
- Android Studio中导入第三方库
之前开发Android都是使用的eclipse,近期因为和外国朋友Timothy一起开发一款应用,他是从WP平台刚切换使用Android的,使用的开发环境时Android Studio,为了便于项目的 ...
- AssetsManager下载类
cocos2dx-2.1.3 2dx自己代的例子进行讲解 360 cocos2dx net --> 2.1.3AssetsManager AppDelegate.cpp详解 1.创建目录 ...
- percona-xtrabackup 文档
https://www.percona.com/doc/percona-xtrabackup/2.4/index.html
- 用Systemtap探索MySQL
http://www.actionsky.com/docs/archives/168#Systemtap 目录 1 Systemtap 2 Systemtap 观测点的支持程度 2.1 官方编译的My ...
- Linux文件系统的几个性能测试软件小结
曾经测试Linux系统下的分布式集群系统的性能,使用了一些测试软件,公司让我给部门同事做一次基础培训,于是翻看以前所写的记录资料挑选了其中几个,所记之处并不完全,只记录使用的功能. 1.Iozone ...
- 标准库 - fmt/format.go 解读
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a B ...
- jQuery.inArray 方法的实现
jQuery.inArray(value, array) 如果 value 在 array 中,则返回该 value 所在的位置.否则返回 -1.这个简单的函数实际上是模拟 Array.prototy ...
- 10 ways to be a faster code reviewer--reference
reference:http://blog.codacy.com/top-10-faster-code-reviews/ This is a blog post of our Code Reading ...