将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; ...
随机推荐
- Merge Sorted Array [LeetCode]
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- Reorder List [LeetCode]
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 数据库索引<二> 如何创建索引
前面一篇说法了索引结构,和几种索引在数据表上的结构,了解了索引可以为查询服务,这篇说一说如何创建索引. >平时可能的创建方式 这个系统中要用到A字段,B字段,C字段做为查询的条件,联接的条件较多 ...
- matalab(绘图)
plot(x) % 绘图语句F = getframe(gcf); %抓取图片imwrite(F.cdata,'myfile.jpg'); %保存图片 然后就可以在默认路径也就是matlab工作目录中找 ...
- PHP获取汉字的转化为拼音字母实现程序
一个完整的php获取汉字拼音字母的实现程序,有需要的朋友可参考一下. <?php class GetPingYing { private $pylist = array( 'a'=>-20 ...
- 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开始服务器进程。请检查用户名和密码。 (异常来自 HRESULT:0x8000401A)。
检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 8000401a 因为配置标识不正确,系统无法开 ...
- js图片轮播图
/*焦点图*/ var Box='.carousel';//盒子 var Menu=$(Box+' .l_cursor li');//圆点菜单 var Con ...
- autoLyout纯代码适配
前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...
- TopCoder SRM 582 ColorfulBuilding
DP 思路是三维,但是时间肯定会超时,需要根据其特殊性质加两个标记数组,优化成二维. 刚开始想了N久N久,没感觉,还是动手画了一下才有用呀,意淫再久,不如动手呀. 代码: #include<i ...
- c# 配置文件之configSections配置
对于小型项目来说,配置信息可以通过appSettings进行配置,而如果配置信息太多,appSettings显得有些乱,而且在开发人员调用时,也不够友好,节点名称很容易写错,这时,我们有几种解决方案 ...