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的更多相关文章

  1. 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel

    步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...

  2. C#_List转换成DataTable

    /// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list" ...

  3. 将List转换成DataTable

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. 将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据

    领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其 ...

  5. C# DataTable转换成实体列表 与 实体列表转换成DataTable

    /// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T"&g ...

  6. 获取报告 Stream转string,利用字符串分割转换成DataTable

    protected void Button1_Click(object sender, EventArgs e) { MemoryStream stream = new MemoryStream(); ...

  7. C#:CsvReader读取.CSV文件并转换成DataTable

    原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...

  8. 将泛类型集合List类转换成DataTable

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  9. C#:CsvReader读取.CSV文件(转换成DataTable)

    原文引用:https://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader using LumenWorks.Framework.IO.Csv; ...

随机推荐

  1. RFID Reader 线路图收集

    This 125 kHz RFID reader http://www.serasidis.gr/circuits/RFID_reader/125kHz_RFID_reader.htm http:// ...

  2. MySQL 127.0.0.1和localhost本质区别

    登录方式: [root@10-4-14-168 ~]# mysql -uroot -p Enter password: 查看权限表 mysql> SELECT user,host,passwor ...

  3. Asp.net使用jQuery实现数据绑定与分页

    使用jQuery来实现Gridview, Repeater等服务器端数据展示控件的数据绑定和分页.本文的关注重点是数据如何实现数据绑定. Content jQuery的强大和可用性使得其迅速的流行起来 ...

  4. [Express] Level 2: Middleware -- 2

    Logging Middleware Help finish the following middleware code in the logger.js file: On the response  ...

  5. iOS开发——UI篇Swift篇&UISegmentedControl

    UISegmentedControl override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...

  6. Cocos2dx 3.0 过渡篇(二十九)globalZOrder()与localZOrder()

    前天非常难得的加班到八点...为什么说难得呢?由于平时我差点儿就没加班过.六点下班后想走就走,想留就留.率直洒脱.不拘一格.尽显男儿本色.程序猿,就是这么自信! -----------这篇博客的标题本 ...

  7. select,epoll的比较

    机制: select:只支持水平触发(数据不处理完无限通知) epoll:支持水平触发和边缘触发(仅通知一次) 单进程监控FD个数 select: 由FD_SETSIZE设置,默认值是2048.在大量 ...

  8. NSOperation使用系统提供子类的方法--处理复杂任务

    //创建一个队列 NSOperationQueue *operation=[[NSOperationQueue alloc]init]; //把任务放在NSBlockOperation里面 NSBlo ...

  9. Java基础知识强化之IO流笔记74:NIO之 Buffer

    Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.这块内存被包装成NIO ...

  10. 标签云 - jqcloud 用法

    html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...