zoukankan      html  css  js  c++  java
  • 将IList<T>转换成DataTable

    public static DataTable Convert<T>(IList<T> list)
            {
                if (list == null || list.Count <= 0)
                {
                    return null;
                }
                DataTable dt = new DataTable(typeof(T).Name);
                DataColumn dc;
                DataRow dr;

                PropertyInfo[] propertyInfo = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

                foreach (T t in list)
                {
                    if (t == null)
                    {
                        continue;
                    }
                    dr = dt.NewRow();
                    for (int i = 0, j = propertyInfo.Length; i < j; i++)
                    {
                        PropertyInfo pi = propertyInfo[i];
                        string name = pi.Name;
                        if (dt.Columns[name] == null)
                        {
                            dc = new DataColumn(name, pi.PropertyType);
                            dt.Columns.Add(dc);
                        }
                        dr[name] = pi.GetValue(t, null);
                    }
                    dt.Rows.Add(dr);
                }
                return dt;
            }

  • 相关阅读:
    Liunx cal
    Liunx read
    IOS
    IOS
    ARPSpoofing教程(四)
    ARPSpoofing教程(三)
    ARPSpoofing教程(二)
    数据结构与算法分析
    hdu 2034
    hdu 2042
  • 原文地址:https://www.cnblogs.com/xiaozhuaweiliang/p/DataTable.html
Copyright © 2011-2022 走看看