将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; ...
随机推荐
- 130. Surrounded Regions -- 被某字符包围的区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 超详细的Xcode代码格式化教程,可自定义样式。
超详细的Xcode代码格式化教程,可自定义样式. 为什么要格式化代码 当团队内有多人开发的时候,每个人写的代码格式都有自己的喜好,也可能会忙着写代码而忽略了格式的问题.在之前,我们可能会写完代码后,再 ...
- python 将数据随机分为训练集和测试集
# -*- coding: utf-8 -*- """ Created on Tue Jun 23 15:24:19 2015 @author: hd "&qu ...
- Github注册流程和使用体验
大家好,我叫施蓓蓓,学号1413042063,在网络工程143班,我的兴趣爱好有很多,特别是在专业方面,比如软件工程.操作系统.网络通信技术.计算机组成原理等,我对游戏十分感兴趣,以后就业会朝这方面发 ...
- vsto publish后无法弹出winform窗口
http://www.cnblogs.com/xiyang1011/archive/2011/06/07/2074025.html - - 没有调用form.show()...
- 使用tomcat配置文件下载服务器,自定义下载列表
先上图,利用tomcat,这个下载界面没有代码,点击文件名即可下载 详细参考:http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html
- 经典DP 二维换一维
HDU 1024 Max Sum Plus Plus // dp[i][j] = max(dp[i][j-1], dp[i-1][t]) + num[j] // pre[j-1] 存放dp[i-1] ...
- [Js]拖拽
分析: 1.鼠标按下,拖拽开始,鼠标移动,拖拽进行,鼠标抬起,拖拽结束(三个事件) 2.被拖动元素与鼠标之间的位置在拖动过程中始终不变,利用这个原理,被拖动元素的位置就是鼠标的左(上)边距-鼠标与被拖 ...
- UVa 10253 - Series-Parallel Networks
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 用PDB库调试Python程序
Python自带的pdb库,发现用pdb来调试程序还是很方便的,当然了,什么远程调试,多线程之类,pdb是搞不定的. 用pdb调试有多种方式可选: 1. 命令行启动目标程序,加上-m参数,这样调用my ...