将List转换成DataTable
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的更多相关文章
- 带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel
步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript&qu ...
- C#_List转换成DataTable
/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list" ...
- list转换成DataTable
list转换成DataTable类如下: public static DataTable ToDataTable<T>(this IList<T> datas) { DataT ...
- 将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; ...
随机推荐
- android关于uses-permission权限列表
在编写Android程序时经常会忘记添加权限,下面是网上收集的关于Androiduses-permission的资料,方便查找~ android.permission.ACCESS_CHECKIN_P ...
- HTML5 视频规范简介
HTML5 视频规范简介 创建于 2013-02-03, 周日 00:56 作者 白建鹏 HTML 一词是“超文本标记语言”(Hyper-Text Markup Language)的缩写,是用于描 ...
- c#获取系统时间的方法(转)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...
- Oracle 权限查询
查看当前用户权限:SQL> select * from session_privs; 查询某个用户被赋予的系统权限. Select * from user_sys_privs; 或者: sele ...
- .net调用存储过程碰到的一个问题
问题描述 报错信息如下: Execution of user code in the .NET Framework is disabled. Enable "clr enabled" ...
- 使用SMSManager短信管理器实现短信群发
import java.util.ArrayList; import android.os.Bundle;import android.provider.ContactsContract;import ...
- [Jquery]滑动门效果
$(function(){ var $box=$("#box"); var $img=$box.find("img"); var imgWid ...
- 【codevs1036】商务旅行 LCA 倍增
1036 商务旅行 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的 ...
- 怎么用navicat自动备份mysql数据库
打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击“计划”,再点击“新建批处理作业”. 双击上面的可用任务,它就会到下面的列表里去,代表你选择了这个任务. 点击保存, ...
- [转]shell基本算术运算
from:http://www.cnblogs.com/yfanqiu/archive/2012/05/10/2494031.html#undefined shell程序中的操作默认都是字符串操作,在 ...