zoukankan      html  css  js  c++  java
  • C#将List集合类转换成DataTable-帮助类

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    
    namespace LifeDecidesHappiness.Net.Utility.ListDataTable
    {
        /// <summary>
        ///     List集合 转 DataTable 帮助类
        ///     https://www.cnblogs.com/LifeDecidesHappiness/p/15273203.html
        ///     LDH @ 2021-9-15
        /// </summary>
        public class List2DataTableHelper
        {
            /// <summary>
            ///     将集合转为DataTable
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list"></param>
            /// <returns></returns>
            public static DataTable ToDataTable<T>(IEnumerable<T> list)
            {
                // 创建属性的集合    
                var pList = new List<PropertyInfo>();
    
                // 获得反射的入口    
                var type = typeof(T);
                var dt = new DataTable();
    
                // 把所有的public属性加入到集合 并添加DataTable的列    
                Array.ForEach(type.GetProperties(), p =>
                {
                    pList.Add(p);
                    dt.Columns.Add(p.Name, p.PropertyType);
                });
    
                foreach (var item in list)
                {
                    // 创建一个DataRow实例    
                    var row = dt.NewRow();
    
                    // 给row 赋值    
                    pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
    
                    // 加入到DataTable    
                    dt.Rows.Add(row);
                }
    
                return dt;
            }
    
            /// <summary>
            ///     将List集合类转换成DataTable
            /// </summary>
            /// <param name="list">集合</param>
            /// <returns></returns>
            public static DataTable List2DataTable(IList list)
            {
                var result = new DataTable();
                if (list.Count > 0)
                {
                    var properties = list[0].GetType().GetProperties();
    
                    foreach (var pi in properties) result.Columns.Add(pi.Name, pi.PropertyType);
                    foreach (var t in list)
                    {
                        var tempList = new ArrayList();
                        foreach (var pi in properties)
                        {
                            var obj = pi.GetValue(t, null);
                            tempList.Add(obj);
                        }
    
                        var array = tempList.ToArray();
                        result.LoadDataRow(array, true);
                    }
                }
    
                return result;
            }
    
            #region Convert a List{T} to a DataTable.
    
            /// <summary>
            ///     Convert a List{T} to a DataTable.
            /// </summary>
            public static DataTable ToDataTable<T>(List<T> items)
            {
                var tb = new DataTable(typeof(T).Name);
    
                var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                foreach (var prop in props)
                {
                    var t = GetCoreType(prop.PropertyType);
                    tb.Columns.Add(prop.Name, t);
                }
    
                foreach (var item in items)
                {
                    var values = new object[props.Length];
    
                    for (var 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;
                    return Nullable.GetUnderlyingType(t);
                }
    
                return t;
            }
    
            #endregion
        }
    }
    踏实做一个为人民服务的搬运工!
  • 相关阅读:
    投稿007期|令人震惊到发指的PyObject对象代码设计之美
    使用OpenCV通过摄像头捕获实时视频并探测人脸
    洛谷 P1259【黑白棋子的移动】
    入门OJ 1281【营救(save)】
    入门OJ 3204【射击】
    POJ 3126【长度为素数的路径个数】
    POJ 1980【Unit Fraction Partition】
    洛谷 P2374【搬运工】
    【常用算法总结——记忆化搜索】
    P3052 [USACO12MAR]【摩天大楼里的奶牛(Cows in a Skyscraper)】
  • 原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/15273203.html
Copyright © 2011-2022 走看看