C# Model实体类 转换 DataTable,List<>集合 转换 DataTable

发布时间:2020-11-30 11:57 作者:独孤剑 阅读:167

C# Model实体类 转换 DataTable,List<>集合 转换 DataTable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            User user = new User();
            user.UserName = "用户名";
            user.Password = "密码";
            user.Age = 20;

            List<User> users = new List<User>();
            for (int i = 0; i < 5; ++i)
            {
                user = new User();
                user.UserName = "用户名" + i.ToString();
                user.Password = "密码" + i.ToString();
                user.Age = 20;

                users.Add(user);
            }

            // Model -> DataTable
            DataTable dt1 = ModelToDataTable<User>(user);

            // List<> -> DataTable
            DataTable dt2 = ListToDataTable<User>(users);
        }


        /// <summary>
        /// Model -> DataTable
        /// </summary>
        /// <typeparam name="T">数据项</typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataTable ModelToDataTable<T>(T data)
        {
            try
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
                DataTable dt = new DataTable();

                // 创建栏
                for (int i = 0; i < properties.Count; i++)
                {
                    PropertyDescriptor property = properties[i];
                    dt.Columns.Add(property.Name, property.PropertyType);
                }
                object[] values = new object[properties.Count];

                // 赋值
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = properties[i].GetValue(data);
                }
                dt.Rows.Add(values);

                return dt;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// List -> DataTable
        /// </summary>
        /// <typeparam name="T">数据项</typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(IList<T> list)
        {
            try
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
                DataTable dt = new DataTable();

                // 创建栏
                for (int i = 0; i < properties.Count; i++)
                {
                    PropertyDescriptor property = properties[i];
                    dt.Columns.Add(property.Name, property.PropertyType);
                }
                object[] values = new object[properties.Count];

                // 赋值
                foreach (T item in list)
                {
                    for (int i = 0; i < values.Length; i++)
                    {
                        values[i] = properties[i].GetValue(item);
                    }
                    dt.Rows.Add(values);
                }

                return dt;
            }
            catch
            {
                throw;
            }
        }
    }

    /// <summary>
    /// 用户
    /// </summary>
    public class User
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }
    }
}

微信打赏, 微信扫一扫

支付宝打赏, 支付宝扫一扫

如果文章对您有帮助,欢迎给作者打赏

作者最新文章
用户id取模分库分表的策略方法
网信办征求意见:收集个人汽车数据信息应取得同意
拜登称政府正调查燃油公司黑客攻击事件 确定该公司网络被勒索软件感染
云南大理森林消防直升机坠入洱海:4名机组人员不幸遇难
jquery $.ajax()方法参数详解
企业信息查询系统