zoukankan      html  css  js  c++  java
  • 将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;
            }
        }
    }
  • 相关阅读:
    [buuctf] pwn-第五空间2019pwn
    [buuctf] pwn-[OGeek2019]babyrop
    [buuctf] pwn-ciscn_2019_c_1
    [buuctf] pwn-jarvisoj_level0
    wamp集成环境配置php7.x连接mssql
    EXCEL小技巧之单击单元格实现自增
    Asuhe博客转移
    数据链路层中的最小帧长是如何计算出来的?
    CSMA/CD协议中如何确定数据重传时机?
    Cache设计
  • 原文地址:https://www.cnblogs.com/slu182/p/4252726.html
Copyright © 2011-2022 走看看