zoukankan      html  css  js  c++  java
  • List 高效转成 Datatable

     public static DataTable ToDataTable<T>(this IList<T> data)
            {
                PropertyDescriptorCollection props =
                    TypeDescriptor.GetProperties(typeof(T));
                DataTable table = new DataTable();
                for (int i = 0; i < props.Count; i++)
                {
                    PropertyDescriptor prop = props[i];
                    table.Columns.Add(prop.Name, prop.PropertyType);
                }
                object[] values = new object[props.Count];
                foreach (T item in data)
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = props[i].GetValue(item);
                    }
                    table.Rows.Add(values);
                }
                return table;
            }
  • 相关阅读:
    javascript变量
    javascript数据类型
    javascript基本语法
    javascript用法
    javascript简介
    js 随机生成颜色值
    JS 判断传入的变量类型是否是Array
    swiper2 swiper-slide 之间的间距调整
    IE9以及以下不支持jquery ajax跨域问题
    HBuilder只提示html 不提示js
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2490420.html
Copyright © 2011-2022 走看看