wyb
2021-05-11 49ce087bd2a34a150597e1cc1da157af242c0b6d
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
 
namespace Pcb.Common
{
    /// <summary>
    /// 对象扩展类
    /// </summary>
    public static class DataExtension
    {
        #region 戴雁冰扩展
        /// <summary>
        /// 将DataRow映射到指定类型中
        /// create by 戴雁冰
        /// </summary>
        /// <typeparam name="TOut">新对象类型</typeparam>
        /// <param name="Src">当前对象</param>
        public static List<TOut> _Map<TOut>(this DataTable Src) where TOut : class
        {
            var tgttype = typeof(TOut);
            var lst = new List<TOut>();
            //读取属性和列的交集
            var names = new HashSet<string>();
            var colnames = new HashSet<string>();
            foreach (DataColumn col in Src.Columns)
            {
                colnames.Add(col.ColumnName);
            }
            var pts = tgttype.GetProperties().Where(t => t.CanWrite).ToDictionary(t => t.Name);
            foreach (var name in colnames)
            {
                if (pts.ContainsKey(name)) names.Add(name);
            }
 
            //赋值数据
            foreach (DataRow row in Src.Rows)
            {
                var tgt = Activator.CreateInstance<TOut>();
                lst.Add(tgt);
                foreach (var name in names)
                {
                    var val = row[name];
                    if (val == DBNull.Value) continue;
                    var pt = pts[name];
                    pt.SetValue(tgt, val._Convert(pt.PropertyType), null);
                }
            }
            return lst;
        }
        /// <summary>
        /// 将DataRow映射到指定类型中
        /// create by 戴雁冰
        /// </summary>
        /// <typeparam name="TOut">新对象类型</typeparam>
        /// <typeparam name="TAttr"></typeparam>
        /// <param name="Src">数据表</param>
        /// <param name="DicParsePropertFunc">分析属性函数集</param>
        /// <returns></returns>
        public static List<TOut> _Map<TOut, TAttr>(this DataTable Src, Dictionary<string, Func<dynamic, TAttr>> DicParsePropertFunc) where TOut : class
        {
            var tgttype = typeof(TOut);
            var lst = new List<TOut>();
            //读取属性和列的交集
            var names = new HashSet<string>();
            var colnames = new HashSet<string>();
            foreach (DataColumn col in Src.Columns)
            {
                colnames.Add(col.ColumnName);
            }
            var pts = tgttype.GetProperties().Where(t => t.CanWrite).ToDictionary(t => t.Name);
            foreach (var name in colnames)
            {
                if (pts.ContainsKey(name)) names.Add(name);
            }
 
            //赋值数据
            Func<dynamic, TAttr> fget;
            foreach (DataRow row in Src.Rows)
            {
                var tgt = Activator.CreateInstance<TOut>();
                lst.Add(tgt);
                foreach (var name in names)
                {
                    var val = row[name];
                    if (val == DBNull.Value) continue;
                    if (DicParsePropertFunc.TryGetValue(name, out fget))
                    {
                        val = fget(val);
                    }
                    var pt = pts[name];
                    pt.SetValue(tgt, val._Convert(pt.PropertyType), null);
                }
            }
            return lst;
        }
        /// <summary>
        /// 将DataRow映射到指定类型中
        /// create by 戴雁冰
        /// </summary>
        /// <typeparam name="TOut">新对象类型</typeparam>
        /// <param name="Src">当前对象</param>
        /// <param name="Tgt">目标对象,null则创建新对象</param>
        public static TOut _Map<TOut>(this DataRow Src, TOut Tgt = null) where TOut : class
        {
            if (Tgt == null) { Tgt = Activator.CreateInstance<TOut>(); }
 
            var tgttype = typeof(TOut);
            var lst = new List<TOut>();
            //读取属性和列的交集
            var names = new HashSet<string>();
            var colnames = new HashSet<string>();
            foreach (DataColumn col in Src.Table.Columns)
            {
                colnames.Add(col.ColumnName);
            }
            var pts = tgttype.GetProperties().Where(t => t.CanWrite).ToDictionary(t => t.Name);
            foreach (var name in colnames)
            {
                if (pts.ContainsKey(name)) names.Add(name);
            }
 
            //赋值数据
            foreach (var name in names)
            {
                var val = Src[name];
                if (val == DBNull.Value) continue;
                var pt = pts[name];
                pt.SetValue(Tgt, val._Convert(pt.PropertyType), null);
            }
            return Tgt;
        }
 
        #region 转换成列表
        /// <summary>
        /// 转换成列表
        /// </summary>
        /// <typeparam name="T">列表项类型</typeparam>
        /// <param name="List">枚举列表</param>
        /// <returns>列表接口</returns>
        public static List<T> _ToList<T>(this IEnumerable<T> List)
        {
            List<T> lst = new List<T>();
            foreach (var i in List) { lst.Add(i); }
            return lst;
        }
        /// <summary>
        /// 转换成列表
        /// </summary>
        /// <param name="List">枚举列表</param>
        /// <returns>列表接口</returns>
        public static List<T> _ToList<T>(this IEnumerable List)
        {
            List<T> lst = new List<T>();
            foreach (T i in List) { lst.Add(i); }
            return lst;
        }
        /// <summary>
        /// 转换成其它类型列表
        /// </summary>
        /// <typeparam name="T">列表项类型</typeparam>
        /// <typeparam name="TValue">其它类型</typeparam>
        /// <param name="List">枚举列表</param>
        /// <param name="Select">选择键函数</param>
        /// <returns>列表</returns>
        public static List<TValue> _ToList<T, TValue>(this IEnumerable<T> List, Func<T, TValue> Select)
        {
            List<TValue> lst = new List<TValue>();
            foreach (var i in List) { lst.Add(Select(i)); }
            return lst;
        }
        /// <summary>
        /// 转换成其它类型列表
        /// </summary>
        /// <typeparam name="T">列表项类型</typeparam>
        /// <typeparam name="TValue">其它类型</typeparam>
        /// <param name="List">枚举列表</param>
        /// <param name="Select">选择键函数</param>
        /// <returns>列表</returns>
        public static List<TValue> _ToList<T, TValue>(this IEnumerable List, Func<T, TValue> Select)
        {
            List<TValue> lst = new List<TValue>();
            foreach (T i in List) { lst.Add(Select(i)); }
            return lst;
        }
        /// <summary>
        /// 转换成其它类型列表
        /// </summary>
        /// <typeparam name="TValue">其它类型</typeparam>
        /// <param name="List">数据行列表</param>
        /// <param name="Select">选择键函数</param>
        /// <returns>列表</returns>
        public static List<TValue> _ToList<TValue>(this System.Data.DataRowCollection List, Func<System.Data.DataRow, TValue> Select)
        {
            List<TValue> lst = new List<TValue>();
            foreach (System.Data.DataRow i in List) { lst.Add(Select(i)); }
            return lst;
        }
        /// <summary>
        /// 转换成其它类型列表
        /// </summary>
        /// <typeparam name="TValue">其它类型</typeparam>
        /// <param name="List">数据行列表</param>
        /// <param name="Select">选择键函数</param>
        /// <returns>列表</returns>
        public static List<TValue> _ToList<TValue>(this System.Data.DataColumnCollection List, Func<System.Data.DataColumn, TValue> Select)
        {
            List<TValue> lst = new List<TValue>();
            foreach (System.Data.DataColumn i in List) { lst.Add(Select(i)); }
            return lst;
        }
        #endregion
 
        #endregion
    }
}