SqlDataReader生成动态Lambda表达式

上一扁使用动态lambda表达式来将DataTable转换成实体,比直接用反射快了不少。主要是首行转换的时候动态生成了委托。

后面的转换都是直接调用委托,省去了多次用反射带来的性能损失。

今天在对SqlServer返回的流对象 SqlDataReader 进行处理,也采用动态生成Lambda表达式的方式转换实体。

先上一版代码

 1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.Common;
5 using System.Data.SqlClient;
6 using System.Linq;
7 using System.Linq.Expressions;
8 using System.Reflection;
9 using System.Text;
10 using System.Threading.Tasks;
11
12 namespace Demo1
13 {
14 public static class EntityConverter
15 {
16 #region
17 /// <summary>
18 /// DataTable生成实体
19 /// </summary>
20 /// <typeparam name="T"></typeparam>
21 /// <param name="dataTable"></param>
22 /// <returns></returns>
23 public static List<T> ToList<T>(this DataTable dataTable) where T : class, new()
24 {
25 if (dataTable == null || dataTable.Rows.Count <= 0) throw new ArgumentNullException("dataTable", "当前对象为null无法生成表达式树");
26 Func<DataRow, T> func = dataTable.Rows[0].ToExpression<T>();
27 List<T> collection = new List<T>(dataTable.Rows.Count);
28 foreach (DataRow dr in dataTable.Rows)
29 {
30 collection.Add(func(dr));
31 }
32 return collection;
33 }
34
35
36 /// <summary>
37 /// 生成表达式
38 /// </summary>
39 /// <typeparam name="T"></typeparam>
40 /// <param name="dataRow"></param>
41 /// <returns></returns>
42 public static Func<DataRow, T> ToExpression<T>(this DataRow dataRow) where T : class, new()
43 {
44 if (dataRow == null) throw new ArgumentNullException("dataRow", "当前对象为null 无法转换成实体");
45 ParameterExpression parameter = Expression.Parameter(typeof(DataRow), "dr");
46 List<MemberBinding> binds = new List<MemberBinding>();
47 for (int i = 0; i < dataRow.ItemArray.Length; i++)
48 {
49 String colName = dataRow.Table.Columns[i].ColumnName;
50 PropertyInfo pInfo = typeof(T).GetProperty(colName);
51 if (pInfo == null || !pInfo.CanWrite) continue;
52 MethodInfo mInfo = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow), typeof(String) }).MakeGenericMethod(pInfo.PropertyType);
53 MethodCallExpression call = Expression.Call(mInfo, parameter, Expression.Constant(colName, typeof(String)));
54 MemberAssignment bind = Expression.Bind(pInfo, call);
55 binds.Add(bind);
56 }
57 MemberInitExpression init = Expression.MemberInit(Expression.New(typeof(T)), binds.ToArray());
58 return Expression.Lambda<Func<DataRow, T>>(init, parameter).Compile();
59 }
60 #endregion
61 /// <summary>
62 /// 生成lambda表达式
63 /// </summary>
64 /// <typeparam name="T"></typeparam>
65 /// <param name="reader"></param>
66 /// <returns></returns>
67 public static Func<SqlDataReader, T> ToExpression<T>(this SqlDataReader reader) where T : class, new()
68 {
69 if (reader == null || reader.IsClosed || !reader.HasRows) throw new ArgumentException("reader", "当前对象无效");
70 ParameterExpression parameter = Expression.Parameter(typeof(SqlDataReader), "reader");
71 List<MemberBinding> binds = new List<MemberBinding>();
72 for (int i = 0; i < reader.FieldCount; i++)
73 {
74 String colName = reader.GetName(i);
75 PropertyInfo pInfo = typeof(T).GetProperty(colName);
76 if (pInfo == null || !pInfo.CanWrite) continue;
77 MethodInfo mInfo = reader.GetType().GetMethod("GetFieldValue").MakeGenericMethod(pInfo.PropertyType);
78 MethodCallExpression call = Expression.Call(parameter, mInfo, Expression.Constant(i));
79 MemberAssignment bind = Expression.Bind(pInfo, call);
80 binds.Add(bind);
81 }
82 MemberInitExpression init = Expression.MemberInit(Expression.New(typeof(T)), binds.ToArray());
83 return Expression.Lambda<Func<SqlDataReader, T>>(init, parameter).Compile();
84 }
85
86 }
87 }

在上一篇的基础上增加了 SqlDataReader 的扩展方法

以下代码是调用

 1 using System;
2 using System.Collections.Generic;
3 using System.Data;
4 using System.Data.Common;
5 using System.Data.SqlClient;
6 using System.Diagnostics;
7 using System.Linq;
8 using System.Reflection;
9 using System.Text;
10 using System.Threading.Tasks;
11
12 namespace Demo1
13 {
14 class Program
15 {
16 static void Main(string[] args)
17 {
18 String conString = "Data Source=.; Initial Catalog=master; Integrated Security=true;";
19 Func<SqlDataReader, Usr> func = null;
20 List<Usr> usrs = new List<Usr>();
21 using (SqlDataReader reader = GetReader(conString, "select object_id 'ID',name 'Name' from sys.objects", CommandType.Text, null))
22 {
23 while (reader.Read())
24 {
25 if (func == null)
26 {
27 func = reader.ToExpression<Usr>();
28 }
29 Usr usr = func(reader);
30 usrs.Add(usr);
31 }
32 }
33 usrs.Clear();
34 Console.ReadKey();
35 }
36
37 public static SqlDataReader GetReader(String conString, String sql, CommandType type, params SqlParameter[] pms)
38 {
39 SqlConnection conn = new SqlConnection(conString);
40 SqlCommand cmd = new SqlCommand(sql, conn);
41 cmd.CommandType = type;
42 if (pms != null && pms.Count() > 0)
43 {
44 cmd.Parameters.AddRange(pms);
45 }
46 conn.Open();
47 return cmd.ExecuteReader(CommandBehavior.CloseConnection);
48 }
49 }
50 class Usr
51 {
52 public Int32 ID { get; set; }
53 public String Name { get; set; }
54 }
55
56
57 }

目前只能处理sqlserver返回的对象,处理其它数据库本来是想增加 DbDataReader 的扩展方法,但发现动态生成lambda表达式的地方出错,所以先将现在的

最新文章

  1. error LNK2001: 无法解析的外部符号
  2. SQL21日自学通笔记(1)
  3. wordpress 自定义登录表单
  4. Eclipse版本android 65535解决方案(原理等同android studio现在的分包方式)
  5. 如何成为一个Xamarin专家
  6. CSS3弹性伸缩布局(二)——flex布局
  7. java、java -version 可以javac没有内部命令的问题
  8. C++ builder的文件操作
  9. Java学习-012-文件删除实例及源代码
  10. MVC4.0网站发布和部署到IIS7.0上的方法【转:http://www.th7.cn/Program/net/201403/183756.shtml】
  11. git checkout not discard changes
  12. DOM对象控制HTML无素——详解3
  13. Jmeter -- 初体验
  14. 处理事件的方式:两种类的覆盖处理(自己管理,覆盖专用事件函数;自己统一管理,覆盖QWidget::Event通用函数),一种对象的处理(父控件统一管理,即安装过滤器),两种全局处理(QCoreApplication安装过滤器;覆盖notify方法)
  15. 【Spring】渲染Web视图
  16. angular路由详解三(路由参数传递)
  17. Spring:AOP
  18. (第十三周)Final Review会议
  19. mysql批量修改列名为小写
  20. websocket服务器+客户端

热门文章

  1. 素数表(Eratosthenes)
  2. 【CS Round #48 (Div. 2 only)】Dominant Free Sets
  3. [React] Style the body element with styled-components and &quot;injectGlobal&quot;
  4. 使用wepy开发微信小程序商城第一篇:项目初始化
  5. php课程 1-3 字符串变量输出方式有哪些(总结:四种)
  6. Elasticsearch 2.3.2 从oracle中同步数据
  7. 【t043】成绩查询
  8. [PReact] Use Link State to Automatically Handle State Changes
  9. Setup iOS Development Environment.
  10. 【37.38%】【codeforces 722C】Destroying Array