• 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
        }
    }
    踏实做一个为人民服务的搬运工!
  • 相关阅读:
    多线程09-Lock和Condition
    多线程08-Callable和Future
    多线程07-线程池
    在linux环境下搭建java web测试环境(非常详细!)
    Linux下的环境部署和项目发布
    Linux下安装软件命令详解
    Linux下安装Tomcat服务器和部署Web应用
    monkey实战--测试步骤、常用参数、常规monkey命令
    如何做好回归测试
    Apache转发到Tomcat
  • 原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/15273203.html
Copyright © 2020-2023  润新知