123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Data;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace WMS.Util
- {
- public class DataTableToList<T> where T : new()
- {
- public static IList<T> ConvertToModel(DataTable dt)
- {
- // 定义集合
- IList<T> ts = new List<T>();
- // 获得此模型的类型
- Type type = typeof(T);
- string tempName = "";
- foreach (DataRow dr in dt.Rows)
- {
- T t = new T();
- // 获得此模型的公共属性
- PropertyInfo[] propertys = t.GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- tempName = pi.Name; // 检查DataTable是否包含此列
- if (dt.Columns.Contains(tempName))
- {
- // 判断此属性是否有Setter
- if (!pi.CanWrite) continue;
- object value = dr[tempName];
- if (value != DBNull.Value)
- pi.SetValue(t, value, null);
- }
- }
- ts.Add(t);
- }
- return ts;
- }
- }
- public static class DataTableExtension
- {
- public static List<T> ToList<T>(this DataTable dt, IRowMapper<T> rowMapper)
- {
- if (dt == null || dt.Rows.Count == 0)
- {
- return null;
- }
- List<T> entites = new List<T>();
- foreach (DataRow dr in dt.Rows)
- {
- var t = rowMapper.MapRow(dr);
- entites.Add(t);
- }
- return entites;
- }
- /// <summary>
- /// 可以直接使用newtonsoft进行序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dt"></param>
- /// <param name="rowMapper"></param>
- /// <returns></returns>
- public static string ToJson<T>(this DataTable dt, IRowMapper<T> rowMapper)
- {
- if (dt == null || dt.Rows.Count == 0)
- {
- return null;
- }
- StringBuilder strBuilder = new StringBuilder("[");
- foreach (DataRow dr in dt.Rows)
- {
- var t = rowMapper.MapRow(dr);
- strBuilder.Append(t + ",");
- }
- strBuilder.Remove(strBuilder.Length - 1, 1);
- strBuilder.Append("]");
- return strBuilder.ToString();
- }
- }
- public class ColumnAttributeMapper<T> : IRowMapper<T>
- {
- private Dictionary<string, Dictionary<string, string>> ColumnPropertyMapper = new Dictionary<string, Dictionary<string, string>>();
- public ColumnAttributeMapper()
- {
- if (!ColumnPropertyMapper.ContainsKey(typeof(T).Name))
- {
- Dictionary<string, string> dict = new Dictionary<string, string>();
- var props = typeof(T).GetProperties();
- foreach (var prop in props)
- {
- var attribute = prop.GetCustomAttributes(true).OfType<ColumnAttribute>().FirstOrDefault();
- dict.Add(attribute.Name, prop.Name);
- }
- ColumnPropertyMapper.Add(typeof(T).Name, dict);
- }
- }
- public T MapRow(DataRow dr)
- {
- T t = (T)Activator.CreateInstance(typeof(T));
- for (int i = 0; i < dr.Table.Columns.Count; i++)
- {
- if (ColumnPropertyMapper.ContainsKey(t.GetType().Name))
- {
- var dict = ColumnPropertyMapper[t.GetType().Name];
- var property = dict[dr.Table.Columns[i].ColumnName];
- PropertyInfo propertyInfo = t.GetType().GetProperty(property);
- if (propertyInfo != null && dr[i] != DBNull.Value)
- propertyInfo.SetValue(t, dr[i], null);
- }
- }
- return t;
- }
- }
- /// <summary>
- /// 将查询结果映射为对象
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- public interface IRowMapper<out TResult>
- {
- /// <summary>
- /// When implemented by a class, returns a new TResult based on row.
- /// </summary>
- /// <param name="row"> The System.Data.IDataRecord to map.</param>
- /// <returns>The instance of TResult that is based on row.</returns>
- TResult MapRow(DataRow dr);
- /// <summary>
- /// 增加自定义转换,todo 复杂列(eee#bb)转换
- /// </summary>
- /// <param name="convert"></param>
- //void AddCustomConverter(Converter convert);
- /// <summary>
- /// 表信息,每个数据库一个
- /// </summary>
- //TablesInfo TablesInfo { get; set; }
- }
- public class ColumnConverter
- {
- public string ColumnName { get; set; }
- public string PropertyPath { get; set; }
- /// <summary>
- /// 数据库值转换为对象值
- /// </summary>
- public Func<object> Convert { get; set; }
- }
- public class JsonMapper : IRowMapper<StringBuilder>
- {
- public StringBuilder MapRow(DataRow dr)
- {
- StringBuilder strBuilder = new StringBuilder();
- StringWriter strWriter = new StringWriter(strBuilder);
- using (JsonWriter writer = new JsonTextWriter(strWriter))
- {
- writer.WriteStartObject();
- foreach (var col in dr.Table.Columns)
- {
- var value = dr.Field<object>(col.ToString());
- writer.WritePropertyName(col.ToString());
- writer.WriteValue(value);
- }
- writer.WriteEndObject();
- }
- return strBuilder;
- }
- }
- public class ColumnAttribute : Attribute
- {
- public string Name { get; set; }
- public ColumnAttribute(string fieldName)
- {
- this.Name = fieldName;
- }
- }
- }
|