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
|
}
|
}
|