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;
            }

  • 相关阅读:
    存图---链式前向星
    Codeforces Round #664 (Div. 2)(A B C D)
    Go Running HDU
    Total Eclipse HDU
    Little W and Contest HDU
    2018 ICPC Asia Nanjing Regional Contest
    Codeforces Round #662 (Div. 2)(A B C D)
    Codeforces Round #661 (Div. 3)(A B C D E)
    素数判断(欧拉筛)
    网络流专题 模板 + 例题 (Going Home POJ
  • 原文地址:https://www.cnblogs.com/wangjingblogs/p/2259990.html
Copyright © 2011-2022 走看看