zoukankan      html  css  js  c++  java
  • Linq 结果转换为DataTable

             /// <summary>
             ///
             /// </summary>
             /// <param name="list"></param>
             /// <returns></returns>
            private System.Data.DataTable GetDataTable(IEnumerable list)
            {
                System.Data.DataTable dt = new System.Data.DataTable();
                bool schemaIsBuild = false;
                PropertyInfo[] props = null;

                foreach (object item in list)
                {
                    if (!schemaIsBuild)
                    {
                        props = item.GetType().GetProperties();
                        foreach (var pi in props)
                            dt.Columns.Add(new DataColumn(pi.Name, pi.PropertyType));
                    }

                    schemaIsBuild = true;

                    var row = dt.NewRow();
                    foreach (var pi in props)
                    {
                        row[pi.Name] = pi.GetValue(item, null);
                    }
                    dt.Rows.Add(row);
                    dt.AcceptChanges();
                }
                return dt;
            }

  • 相关阅读:
    Mysql数据库的一些操作
    【狂神说Java】JavaWeb入门到实战1---笔记
    图神经网络学习
    HWSX网址
    python如何判断两个数组完全相等?
    SQL-3-菜鸟教程
    SQL-2
    时间序列相似度分析算法
    pip install 安装不了怎么办?
    leetcode 3 无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/wangjingblogs/p/2259990.html
Copyright © 2011-2022 走看看