zoukankan      html  css  js  c++  java
  • [C#] 将 List 转 DataTable

    /// <summary>
            /// Convert a List{T} to a DataTable.
            /// </summary>
            private DataTable ToDataTable<T>(List<T> items)
            {
                var tb = new DataTable(typeof(T).Name);
    
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                foreach (PropertyInfo prop in props)
                {
                    Type t = GetCoreType(prop.PropertyType);
                    tb.Columns.Add(prop.Name, t);
                }
    
                foreach (T item in items)
                {
                    var values = new object[props.Length];
    
                    for (int i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                    }
    
                    tb.Rows.Add(values);
                }
    
                return tb;
            }
    
            /// <summary>
            /// Determine of specified type is nullable
            /// </summary>
            public static bool IsNullable(Type t)
            {
                return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
            }
    
            /// <summary>
            /// Return underlying type if type is Nullable otherwise return the type
            /// </summary>
            public static Type GetCoreType(Type t)
            {
                if (t != null && IsNullable(t))
                {
                    if (!t.IsValueType)
                    {
                        return t;
                    }
                    else
                    {
                        return Nullable.GetUnderlyingType(t);
                    }
                }
                else
                {
                    return t;
                }
            }

      方法2

    public static DataTable ToDataTable<T>(IEnumerable<T> collection)
     {
         var props = typeof(T).GetProperties();
         var dt = new DataTable();
         dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
         if (collection.Count() > 0)
         {
             for (int i = 0; i < collection.Count(); i++)
             {
                 ArrayList tempList = new ArrayList();
                 foreach (PropertyInfo pi in props)
                 {
                     object obj = pi.GetValue(collection.ElementAt(i), null);
                     tempList.Add(obj);
                 }
                 object[] array = tempList.ToArray();
                 dt.LoadDataRow(array, true);
             }
         }
         return dt;
     }
    

      

  • 相关阅读:
    C++Josephus问题
    C++背包示例
    C++1000以内的质数
    as3+asp+access编码
    fb设置flashplayer
    三视图示例
    正确实现 IDisposable 接口
    .net垃圾回收和CLR 4.0对垃圾回收所做的改进之二
    .net垃圾回收和CLR 4.0对垃圾回收所做的改进之三
    CLR 全面透彻解析:大型对象堆揭秘
  • 原文地址:https://www.cnblogs.com/linhuide/p/5941586.html
Copyright © 2011-2022 走看看