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 where T : new() { public static IList ConvertToModel(DataTable dt) { // 定义集合 IList ts = new List(); // 获得此模型的类型 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 ToList(this DataTable dt, IRowMapper rowMapper) { if (dt == null || dt.Rows.Count == 0) { return null; } List entites = new List(); foreach (DataRow dr in dt.Rows) { var t = rowMapper.MapRow(dr); entites.Add(t); } return entites; } /// /// 可以直接使用newtonsoft进行序列化 /// /// /// /// /// public static string ToJson(this DataTable dt, IRowMapper 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 : IRowMapper { private Dictionary> ColumnPropertyMapper = new Dictionary>(); public ColumnAttributeMapper() { if (!ColumnPropertyMapper.ContainsKey(typeof(T).Name)) { Dictionary dict = new Dictionary(); var props = typeof(T).GetProperties(); foreach (var prop in props) { var attribute = prop.GetCustomAttributes(true).OfType().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; } } /// /// 将查询结果映射为对象 /// /// public interface IRowMapper { /// /// When implemented by a class, returns a new TResult based on row. /// /// The System.Data.IDataRecord to map. /// The instance of TResult that is based on row. TResult MapRow(DataRow dr); /// /// 增加自定义转换,todo 复杂列(eee#bb)转换 /// /// //void AddCustomConverter(Converter convert); /// /// 表信息,每个数据库一个 /// //TablesInfo TablesInfo { get; set; } } public class ColumnConverter { public string ColumnName { get; set; } public string PropertyPath { get; set; } /// /// 数据库值转换为对象值 /// public Func Convert { get; set; } } public class JsonMapper : IRowMapper { 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(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; } } }