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;
            }
        }
    }
  • 相关阅读:
    【java基础】(5)静态绑定和动态绑定
    Java的接口和抽象类
    【java基础】(3)Java继承内存分配
    【java基础】(2)Java父类与子类的 内存引用讲解
    [ECMAScript 6]学习小结之---模块
    CDN 内容分发网络
    移动前端开发之viewport的深入理解
    px em rem
    https 结合使用 对称加密和非对称加密
    java Queue中 remove/poll, add/offer, element/peek区别
  • 原文地址:https://www.cnblogs.com/slu182/p/4252726.html
Copyright © 2011-2022 走看看