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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    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;
            }
        }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    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 AOP实现MySQL数据库读写分离案例分析
    mysql的备份(文档的内容小编都已经验证过了)
    【信息化】MySQL数据库简介
    基于MySQL的分布式数据库TDSQL十年锻造经验分享
    MySQL数据库丢失数据场景分析
    微博的MySQL数据库优化实践经验
    数据库MySQL调优实战经验总结
    让数据库跑的更快的7个MySQL优化建议
    一文看懂 MySQL 分区和分表,提高表增删改查效率
    三分钟掌握SQL Server迁移Mongodb
  • 原文地址:https://www.cnblogs.com/wz9003/p/8931821.html
Copyright © 2011-2022 走看看