王 垚
2022-09-05 226dd2b152c0fb06b3f8108ac7740216a8a453c6
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace ZD.Share.Common
{
    public static class DataHelper
    {
        public static List<T> ToModelList<T>(this DataSet ds) where T : new()
        {
            try
            {
                List<T> list = new List<T>();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows) 
                    {
                        list.Add(row.Put(new T()));
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
        public static List<T> ToModelList<T>(this DataTable dt) where T : new()
        {
            try
            {
                List<T> list = new List<T>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        list.Add(row.Put(new T()));
                    }
                }
                return list;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
        /// <summary>
        /// 返回一条数据的Model
        /// </summary>
        /// <typeparam name="T">Model类型</typeparam>
        /// <param name="entity">Model类型</param>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static T ToModel<T>(DataSet ds) where T : new()
        {
            try
            {
                T m = new T();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    m = ds.Tables[0].Rows[0].Put(new T());
                }
                return m;
            }
            catch (Exception)
            {
                return default(T);
                //throw;
            }
        }
 
        public static T Put<T>(this DataRow row, T entity) where T : new()
        {
            if (entity == null)
            {
                entity = new T();
            }
            Type type = typeof(T);
            PropertyInfo[] pi = type.GetProperties();
            foreach (PropertyInfo item in pi)
            {
                int index = row.Table.Columns.IndexOf(item.Name);
                if (index != -1)
                {
                    if (row[item.Name] != null && row[item.Name] != DBNull.Value)
                    {
                        if (item.PropertyType == typeof(DateTime))
                        {
                            //如果对日期格式有特殊要求 可以在这里转换
                            item.SetValue(entity, Convert.ToDateTime(row[item.Name].ToString()), null);
                        }
                        else
                        {
                            item.SetValue(entity, ChangeType(row[item.Name], item.PropertyType), null);
                        }
                    }
                }
            }
            return entity;
        }
 
        public static object ChangeType(object obj, Type conversionType)
        {
            return ChangeType(obj, conversionType, Thread.CurrentThread.CurrentCulture);
        }
 
        public static object ChangeType(object obj, Type conversionType, IFormatProvider provider)
        {
 
            #region Nullable
            Type nullableType = Nullable.GetUnderlyingType(conversionType);
            if (nullableType != null)
            {
                if (obj == null)
                {
                    return null;
                }
                return Convert.ChangeType(obj, nullableType, provider);
            }
            #endregion
            if (typeof(System.Enum).IsAssignableFrom(conversionType))
            {
                return Enum.Parse(conversionType, obj.ToString());
            }
            return Convert.ChangeType(obj, conversionType, provider);
        }
 
 
 
        public static string ToListParam<T>(this List<T> list)
        {
            string str = string.Empty;
            foreach (var item in list)
            {
                str += $"'{item}',";
            }
            str = str.TrimEnd(',');
            str = $"({str})";
            return str;
        }
 
 
        public static string GetUpdateStr(string table, Dictionary<string, object> dic, string condition)
        {
            var strSet = string.Empty;
            foreach (var item in dic)
            {
                var type = Util.GetObjectType(item.Value);
                switch (type)
                {
                    case "string":
                    case "date":
                        strSet += $" {item.Key}='{item.Value}' ";
                        break;
                    default:
                        strSet += $" {item.Key}={item.Value} ";
                        break;
                }
            }
            return $"update {table} set {strSet} where 1=1 {condition}";
        }
    }
}