Asp. NET example of a dynamic lambda expression generated in SqlDataReader

Source: Internet
Author: User
This article mainly introduces the SqlDataReader generated dynamic lambda expression, the need for friends can refer to the following

Previous flat using a dynamic lambda expression to convert a DataTable to an entity is much faster than using reflection directly. The principal is the dynamic generation of the delegate when the first line is converted.

The subsequent conversions are direct invocation of the delegate, eliminating the performance penalty caused by multiple reflections.

Today, the stream object SqlDataReader returned by SQL Server is processed, and the entity is transformed by dynamically generating a lambda expression.

First version of the code

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>//Dat Atable generate entities///</summary>//<typeparam name= "T" ></typeparam>//<param name= "dataTable" >&lt ;/param>//<returns></returns> public static list<t> tolist<t> (This DataTable DataTable) W Here T:class, new () {if (dataTable = = NULL | | DataTable.Rows.Count <= 0) throw new ArgumentNullException ("Datatab   Le "," the current object is null cannot generate an expression tree "); 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>//Generate an expression</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", "current object is null cannot be converted to entity");   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>//Generate lambda expression///</summary>//<typeparam name= "T" ></typeparam&gt  ; <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", "Invalid current object");   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 (); } }}

The extension method of SqlDataReader is added on the basis of the previous article.

The following code is called

Using system;using system.collections.generic;using system.data;using system.data.common;using System.data.sqlclient;using system.diagnostics;using system.linq;using system.reflection;using System.Text;using System.threading.tasks;namespace demo1{class Program {static void Main (string[] args) {string constring = "Data Sou Rce=.; Initial Catalog=master;   Integrated security=true; ";   Func<sqldatareader, usr> func = null;   list<usr> Usrs = new list<usr> (); using (SqlDataReader reader = Getreader (constring, "SELECT object_id ' id '," Name ' name ' from sys.objects ", CommandType.Text , null)) {while (reader. Read ()) {if (Func = = null) {func = reader.     Toexpression<usr> ();     } usr usr = func (reader); Usrs.    Add (USR); }} Usrs.   Clear ();  Console.readkey (); } public static SqlDataReader Getreader (string constring, String sql, CommandType type, params sqlparameter[] PMs) {S   Qlconnection conn = new SqlConnection (constring); SqlCommand cmd = new SqlCommand (SQL, conn);   Cmd.commandtype = type; if (PMS! = null && PMS. Count () > 0) {cmd.   Parameters.addrange (PMS); } conn.   Open (); return CMD.  ExecuteReader (commandbehavior.closeconnection);  }} class Usr {public Int32 ID {get; set;} Public String Name {get; set;}}}

Currently, only the objects returned by SQL Server can be processed, and the other database is intended to increase the DbDataReader extension method, but it is found that there is an error in generating the lambda expression dynamically, so the current

The program is documented.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.