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. jQuery性能优化的28个建议

    我一直在寻找有关jQuery性能优化方面的小窍门,能让我那臃肿的动态网页应用变得轻便些.找了很多文章后,我决定将最好最常用的一些优化性能的建议列出来.我也做了一个jQuery性能优化的简明样式表,你可 ...

  2. CSS实现标题右侧“更多”

    HTML < h2>< a h ref="#" >标题< /a> < span>更多…< /span> < /h2 ...

  3. HTML 转义符

    特殊字符 字符 十进制 转义字符 “ " " & & & < < < > > > 不断开空格(non-breaking ...

  4. Linux中搭建SVNserver

    一 下载工具 1. subversion-1.6.17.tar.gz 2. subversion-deps-1.6.17.tar.gz 二 解压两个包: 1.在此之前,我已经创建了一个用户svnroo ...

  5. ios实例开发精品源码文章推荐

    iOS源码:游戏引擎-推箱子游戏 http://www.apkbus.com/android-106392-1-11.html iOS源码:进度条-Colorful ProgressView http ...

  6. Android Checkbox Example

    1. Custom String 打开 “res/values/strings.xml” 文件, File : res/values/strings.xml <?xml version=&quo ...

  7. 在C++中使用golang的协程

    开源项目cpp_features提供了一个仿golang协程的stackful协程库. 可以在c++中使用golang的协程,大概语法是这样的: #include <iostream> v ...

  8. Java Web目录

    1. Spring持久化 2. Spring核心之IoC——依赖注入 3. Hibernate查询语言 4. Hibernate 实体关联关系映射(转载) 5. 用MyEclipse自动生成hiber ...

  9. 关于解决 The processing instruction target matching "[xX][mM][lL]" is not allowed

    在处理和保存XML文件时,出现The processing instruction target matching "[xX][mM][lL]" is not allowed 错误 ...

  10. C#操作串口总结

    Technorati 标签: C#,SerialPort,ReadTo,ReadTimeout        最近几天一直在调一个要长时间连续不断的操作串口,并且是多线程运行,不允许中断的服务.后来服 ...