zoukankan      html  css  js  c++  java
  • IList To DataTable

    public DataTable IListOut(IList<ViewUserInfo> ResList)
        {
            DataTable TempDT = new DataTable();
    
            //此处遍历IList的结构并建立同样的DataTable
            System.Reflection.PropertyInfo[] p = ResList[0].GetType().GetProperties();
            foreach (System.Reflection.PropertyInfo pi in p)
            {
                TempDT.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
            }
    
            for (int i = 0; i < ResList.Count; i++)
            {
                ArrayList TempList = new ArrayList();
                //将IList中的一条记录写入ArrayList
                foreach (System.Reflection.PropertyInfo pi in p)
                {
                    object oo = pi.GetValue(ResList[i], null);
                    TempList.Add(oo);
                }
    
                object[] itm = new object[p.Length];
                //遍历ArrayList向object[]里放数据
                for (int j = 0; j < TempList.Count; j++)
                {
                    itm.SetValue(TempList[j], j);
                }
                //将object[]的内容放入DataTable
                TempDT.LoadDataRow(itm, true);
            }
            //返回DataTable
            return TempDT;
        }
    
  • 相关阅读:
    HTML-代码定义
    数组
    for。。。for嵌套if,if嵌套for。
    输入年月日, 判断输入的是否正确
    日期功能
    方程
    5.8 一维数组
    5.9 二维数组
    5.7 类
    5.4穷举,迭代
  • 原文地址:https://www.cnblogs.com/hausthy/p/3979193.html
Copyright © 2011-2022 走看看