国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SqlDataReader生成動態Lambda表達式

2024-08-31 00:45:42
字體:
來源:轉載
供稿:網友

上一扁使用動態lambda表達式來將DataTable轉換成實體,比直接用反射快了不少。主要是首行轉換的時候動態生成了委托。

后面的轉換都是直接調用委托,省去了多次用反射帶來的性能損失。

今天在對SqlServer返回的流對象 SqlDataReader 進行處理,也采用動態生成Lambda表達式的方式轉換實體。

先上一版代碼

using System;using System.Collections.Generic;using System.Data;using System.Data.Common;using System.Data.SqlClient;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Text;using System.Threading.Tasks;namespace Demo1{ public static class EntityConverter {  #region  /// <summary>  /// DataTable生成實體  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="dataTable"></param>  /// <returns></returns>  public static List<T> ToList<T>(this DataTable dataTable) where T : class, new()  {   if (dataTable == null || dataTable.Rows.Count <= 0) throw new ArgumentNullException("dataTable", "當前對象為null無法生成表達式樹");   Func<DataRow, T> func = dataTable.Rows[0].ToExpression<T>();   List<T> collection = new List<T>(dataTable.Rows.Count);   foreach (DataRow dr in dataTable.Rows)   {    collection.Add(func(dr));   }   return collection;  }  /// <summary>  /// 生成表達式  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="dataRow"></param>  /// <returns></returns>  public static Func<DataRow, T> ToExpression<T>(this DataRow dataRow) where T : class, new()  {   if (dataRow == null) throw new ArgumentNullException("dataRow", "當前對象為null 無法轉換成實體");   ParameterExpression parameter = Expression.Parameter(typeof(DataRow), "dr");   List<MemberBinding> binds = new List<MemberBinding>();   for (int i = 0; i < dataRow.ItemArray.Length; i++)   {    String colName = dataRow.Table.Columns[i].ColumnName;    PropertyInfo pInfo = typeof(T).GetProperty(colName);    if (pInfo == null || !pInfo.CanWrite) continue;    MethodInfo mInfo = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow), typeof(String) }).MakeGenericMethod(pInfo.PropertyType);    MethodCallExpression call = Expression.Call(mInfo, parameter, Expression.Constant(colName, typeof(String)));    MemberAssignment bind = Expression.Bind(pInfo, call);    binds.Add(bind);   }   MemberInitExpression init = Expression.MemberInit(Expression.New(typeof(T)), binds.ToArray());   return Expression.Lambda<Func<DataRow, T>>(init, parameter).Compile();  }  #endregion  /// <summary>  /// 生成lambda表達式  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="reader"></param>  /// <returns></returns>  public static Func<SqlDataReader, T> ToExpression<T>(this SqlDataReader reader) where T : class, new()  {   if (reader == null || reader.IsClosed || !reader.HasRows) throw new ArgumentException("reader", "當前對象無效");   ParameterExpression parameter = Expression.Parameter(typeof(SqlDataReader), "reader");   List<MemberBinding> binds = new List<MemberBinding>();   for (int i = 0; i < reader.FieldCount; i++)   {    String colName = reader.GetName(i);    PropertyInfo pInfo = typeof(T).GetProperty(colName);    if (pInfo == null || !pInfo.CanWrite) continue;    MethodInfo mInfo = reader.GetType().GetMethod("GetFieldValue").MakeGenericMethod(pInfo.PropertyType);    MethodCallExpression call = Expression.Call(parameter, mInfo, Expression.Constant(i));    MemberAssignment bind = Expression.Bind(pInfo, call);    binds.Add(bind);   }   MemberInitExpression init = Expression.MemberInit(Expression.New(typeof(T)), binds.ToArray());   return Expression.Lambda<Func<SqlDataReader, T>>(init, parameter).Compile();  } }}            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 哈巴河县| 黄龙县| 定兴县| 合川市| 霸州市| 蕉岭县| 涡阳县| 包头市| 思南县| 黄梅县| 廊坊市| 读书| 电白县| 东乌| 信阳市| 拉孜县| 甘谷县| 哈尔滨市| 新竹市| 青田县| 建平县| 富宁县| 聊城市| 长治市| 巴林左旗| 凤山市| 汤原县| 阳江市| 平度市| 桂东县| 五常市| 八宿县| 富裕县| 三都| 当雄县| 宁德市| 郎溪县| 韩城市| 娄烦县| 连南| 饶平县|