zoukankan      html  css  js  c++  java
  • DataTable与List<T>相互转换

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Common
    {
        public class ModelHelper
        {
            public static List<T> TableToEntity<T>(DataTable dt) where T : new()
            {
                List<T> lists = new List<T>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        lists.Add(SetVal(new T(), row));
                    }
                }
                return lists;
            }
    
            public static T SetVal<T>(T entity, DataRow row) where T : new()
            {
                Type type = typeof(T);
                PropertyInfo[] pi = type.GetProperties();
                foreach (PropertyInfo item in pi)
                {
                    if (row[item.Name] != null && row[item.Name] != DBNull.Value)
                    {
                        if (item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                        {
                            Type conversionType = item.PropertyType;
                            NullableConverter nullableConverter = new NullableConverter(conversionType);
                            conversionType = nullableConverter.UnderlyingType;
                            item.SetValue(entity, Convert.ChangeType(row[item.Name], conversionType), null);
                        }
                        else
                        {
                            item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
                        }
                    }
                }
                return entity;
            }
    
            public static DataTable EntityToDataTable<T>(List<T> list) where T : new()
            {
                if (list == null || list.Count == 0)
                {
                    return null;
                }
    
                DataTable dataTable = new DataTable(typeof(T).Name);
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                    {
                        Type conversionType = propertyInfo.PropertyType;
                        NullableConverter nullableConverter = new NullableConverter(conversionType);
                        conversionType = nullableConverter.UnderlyingType;
                        dataTable.Columns.Add(new DataColumn(propertyInfo.Name, conversionType));
                    }
                    else
                    {
                        dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
                    }
                }
    
                foreach (T model in list)
                {
                    DataRow dataRow = dataTable.NewRow();
                    foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                    {
                        object value = propertyInfo.GetValue(model, null);
                        if (value != null)
                        {
                            dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                        }
                        else
                        {
                            dataRow[propertyInfo.Name] = DBNull.Value;
                        }
                    }
                    dataTable.Rows.Add(dataRow);
                }
                return dataTable;
            }
        }
    }
  • 相关阅读:
    Spring Boot:拦截器与过滤器
    [Locked] Maximum Size Subarray Sum Equals k
    [Locked] Generalized Abbreviation
    [Locked] Meeting Room I && II
    [Locked] Zigzag Iterator
    [Locked] Paint House I & II
    [Locked] Number of Connected Components in an Undirected Graph
    [Locked] Best Meeting Point
    [Locked] Sparse Matrix Multiplication
    [Locked] Two Sum
  • 原文地址:https://www.cnblogs.com/joryblog/p/8910853.html
Copyright © 2011-2022 走看看