using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using YTO.WeiXin.Model;
using System.Collections; namespace WeiXin.Core
{
public class ListToDataTable
{
public static DataTable ToDataTable<T>(IList<T> list)
{
return ToDataTable(list, null);
} //将list转换成DataTable
public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
{
List<string> propertyNameList = new List<string>();
if (propertyName != null)
{
propertyNameList.AddRange(propertyName);
}
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
}
}
for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
} //AuthorizationInfo 将DataTable转换成list
public static IList<AuthorizationInfo> ToList(DataTable dt, IList<AuthorizationInfo> list1)
{
//IList<AuthorizationInfo> list1 = new List<AuthorizationInfo>();
for (int i = ; i < dt.Rows.Count; i++)
{
AuthorizationInfo authInfo = new AuthorizationInfo();
authInfo.Id = (dt.Rows[i].ItemArray[]).ToString();
authInfo.LiencePlateNumber = (dt.Rows[i].ItemArray[]).ToString();
authInfo.Bar_code = (dt.Rows[i].ItemArray[]).ToString();
authInfo.PhoneNumber = (dt.Rows[i].ItemArray[]).ToString();
authInfo.OpenId = (dt.Rows[i].ItemArray[]).ToString();
authInfo.CreateTime = Convert.ToDateTime(dt.Rows[i].ItemArray[]);
authInfo.Status = (dt.Rows[i].ItemArray[]).ToString();
list1.Add(authInfo);
}
return list1;
} //将ExcptionInfo DataTable转换成list
public static IList<ExcptionInfo> ToList(DataTable dt, IList<ExcptionInfo> list1)
{
//IList<ExcptionInfo> list1 = new List<ExcptionInfo>();
for (int i = ; i < dt.Rows.Count; i++)
{
ExcptionInfo exInfo = new ExcptionInfo();
exInfo.Id = (dt.Rows[i].ItemArray[]).ToString();
exInfo.LiencePlateNumber = (dt.Rows[i].ItemArray[]).ToString();
exInfo.CarLine = (dt.Rows[i].ItemArray[]).ToString();
exInfo.PhoneNumber = (dt.Rows[i].ItemArray[]).ToString();
exInfo.OpenId = (dt.Rows[i].ItemArray[]).ToString();
exInfo.CreateTime = Convert.ToDateTime(dt.Rows[i].ItemArray[]);
exInfo.Remark = (dt.Rows[i].ItemArray[]).ToString();
exInfo.ExcptionCategory = (dt.Rows[i].ItemArray[]).ToString();
exInfo.Position = (dt.Rows[i].ItemArray[]).ToString();
list1.Add(exInfo);
}
return list1;
}
}
}

将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

    list转换成DataTable类如下: public static DataTable ToDataTable<T>(this IList<T> datas) { DataT ...

  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. 为大家分享一个 Ajax Loading —— spin.js(转)

    原文地址:http://www.cnblogs.com/lxblog/p/3425599.html 我们在做Ajax 异步请求的时候,一般都会利用一个动态的 Gif 小图片来制作一个Ajax Load ...

  2. 在jsp页面中实现格式化数字,百分比,货币

    当时的要求是在jsp页面中计算百分比 实现方法 1.引入Jstl的fmt指令 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" ...

  3. Struts2配置拦截器,struts2加载常量时的搜索顺序

    1:struts2加载常量时的搜索顺序 1.Struts-default.xml 2.Struts-plugin.xml 3.Struts.xml 4.Struts-properties(自己创建的) ...

  4. 3.7 嵌入式SQL

    可以放入所有高级语言中去,如C 因为,SQL是过程性语句,需要高级语言的非过程性处理集合的分类处理 一.一般形式 所有的SQL语句都必须加前缀EXEC SQL SQL语句完成结束标志(:或END EX ...

  5. WIN7下更改TFS连接用户的方法

    如果你在第一用VS连接TFS的时候,当你输入完用户名和密码并点击保存时,你的用户登录信息就保存在Credential Manager. 你可以到 Control Panel->User Acco ...

  6. 使用openssl库实现RSA、AES数据加密

         openssl是可以很方便加密解密的库,可以使用它来对需要在网络中传输的数据加密.可以使用非对称加密:公钥加密,私钥解密.openssl提供了对RSA的支持,但RSA存在计算效率低的问题,所 ...

  7. 使用NuGet时的一个乌龙

    问题描述 最近自己做的一个项目,计划开始使用NuGet来管理dll,但是遇到一个奇怪,但是结果证明是个乌龙的问题. 新建一个WebApi项目,使用NuGet管理第三方dll,其中有引用Newtonso ...

  8. GUID

    前言 全局唯一标识符,简称GUID(发音为 /ˈɡuːɪd/或/ˈɡwɪd/),是一种由算法生成的唯一标识,通常表示成32个16进制数字(0-9,A-F)组成的字符串,如:{21EC2020-3AEA ...

  9. Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í??

    2010-5-18 22:00:38 org.apache.catalina.core.AprLifecycleListener lifecycleEvent 信息: The Apache Tomca ...

  10. 使用Matrix控制图像或组件变换的步骤

    1.获取Matrix对象,该Matrix对象既可新创建,也可直接获取其他对象内封装的Matrix(例如Transformation对象内部) 2.调用Matrix的方法进行平移.旋转.缩放.倾斜等. ...