国产97色在线|亚洲-欧美人妻另类制服丝袜-欧美人成国产91视频-殷素素一女战二夫|www.ycjrc.net

C# Model實體類 轉換 DataTable,List<>集合 轉換 DataTable, 字段類型可以是Nullable<>

發布時間:2020-11-30 11:58 作者:獨孤劍 閱讀:1370

C# Model實體類 轉換 DataTable,List<>集合 轉換 DataTable, 字段類型可以是Nullable<>
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);
                    // 當字段類型是Nullable<>時
                    Type type = property.PropertyType;
                    if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        type = type.GetGenericArguments()[0];
                    }
                    dt.Columns.Add(new DataColumn(property.Name, type));
                }
                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);
                    // 當字段類型是Nullable<>時
                    Type type = property.PropertyType;
                    if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        type = type.GetGenericArguments()[0];
                    }
                    dt.Columns.Add(new DataColumn(property.Name, type));
                }
                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; }
    }
}

微信打賞, 微信掃一掃

支付寶打賞, 支付寶掃一掃

如果文章對您有幫助,歡迎給作者打賞

作者最新文章
開發過程中解決360兼容模式瀏覽器的方法
云南象群向西南方向遷移,云南離群獨象距離象群約12公里
吉林做網站最低價格,吉林企業網站建設價格低至500元起
守象人直擊云南象群最新動向
網站影響百度蜘蛛抓取量的因素有哪些?為什么我的網站Baidu蜘蛛來的次數少?
企業名片
在線客服