文档章节

DataTable与List<T>相互转化

o
 osc_z1hvg4cu
发布于 2018/04/24 17:10
字数 620
阅读 0
收藏 0

精选30+云产品,助力企业轻松上云!>>>

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;
         }
     }
}
o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

MongoDB入门系列——3.可视化工具篇

点击上方,轻松关注!! 前面我们已经介绍了MongoDB怎么安装,接下来要安装他的可视化工具——Studio 3T。 先到这下载一个压缩包,百度网盘,https://pan.baidu.com/s/1M8mlWo334KE8I1_UA2Da...

学习Java的小姐姐
2018/11/08
17
0
分层图的绘制 python(来自国外课程)

Exercise 10: Hierarchical clustering of the grain data In the video, you learnt that the SciPy linkage() function performs hierarchical clustering on an array of samples. Use th......

齐勇cn
37分钟前
13
0
微信小程序超简单的双向绑定(类似vue的v-model)

<input model:value="{{value}}" />

祖达
38分钟前
9
0
为什么AngularJS在select中包含一个空选项? - Why does AngularJS include an empty option in select?

问题: I've been working with AngularJS for the last few weeks, and the one thing which is really bothering me is that even after trying all permutations or the configuration de......

技术盛宴
40分钟前
13
0
centos宝塔面板安装及常见错误处理(超级详细)

原文连接:https://www.wjcms.net/archives/centos%E5%AE%9D%E5%A1%94%E9%9D%A2%E6%9D%BF%E5%AE%89%E8%A3%85%E5%8F%8A%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%E8%B6%85%E7%......

神兵小将
今天
17
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部