首页 > 代码库 > 将List转换成DataTable
将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 > 0) { PropertyInfo[] propertys = list[0].GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { result.Columns.Add(pi.Name, pi.PropertyType); } else { if (propertyNameList.Contains(pi.Name)) { result.Columns.Add(pi.Name, pi.PropertyType); } } } for (int i = 0; i < list.Count; i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in propertys) { if (propertyNameList.Count == 0) { 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 = 0; i < dt.Rows.Count; i++) { AuthorizationInfo authInfo = new AuthorizationInfo(); authInfo.Id = (dt.Rows[i].ItemArray[0]).ToString(); authInfo.LiencePlateNumber = (dt.Rows[i].ItemArray[1]).ToString(); authInfo.Bar_code = (dt.Rows[i].ItemArray[2]).ToString(); authInfo.PhoneNumber = (dt.Rows[i].ItemArray[3]).ToString(); authInfo.OpenId = (dt.Rows[i].ItemArray[4]).ToString(); authInfo.CreateTime = Convert.ToDateTime(dt.Rows[i].ItemArray[5]); authInfo.Status = (dt.Rows[i].ItemArray[6]).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 = 0; i < dt.Rows.Count; i++) { ExcptionInfo exInfo = new ExcptionInfo(); exInfo.Id = (dt.Rows[i].ItemArray[0]).ToString(); exInfo.LiencePlateNumber = (dt.Rows[i].ItemArray[1]).ToString(); exInfo.CarLine = (dt.Rows[i].ItemArray[2]).ToString(); exInfo.PhoneNumber = (dt.Rows[i].ItemArray[3]).ToString(); exInfo.OpenId = (dt.Rows[i].ItemArray[4]).ToString(); exInfo.CreateTime = Convert.ToDateTime(dt.Rows[i].ItemArray[5]); exInfo.Remark = (dt.Rows[i].ItemArray[8]).ToString(); exInfo.ExcptionCategory = (dt.Rows[i].ItemArray[6]).ToString(); exInfo.Position = (dt.Rows[i].ItemArray[7]).ToString(); list1.Add(exInfo); } return list1; } }}
将List转换成DataTable
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。