Saturday, September 25, 2010

LINQ advanced features brief



This article describes the advanced features of LINQ, which includes a concern for all usage and the use of dynamic query IQueryable expression trees based on the data source to construct a dynamic query.

Advanced features of the dynamic query LINQ

Such a scenario: the application may provide a user interface, users can specify the user interface one or more predicates to filter the data. This situation did not know at compile time check the details of dynamic query will be very useful.

In LINQ in, Lambda expressions are a number of the basis of standard query operators, the compiler creates lambda expression to capture the underlying query methods (such as Where, Select, Order By, Take While, and other methods) defined in the calculations. Expression tree structure of the data source used for the query, the data source to achieve IQueryable. For example, LINQ to SQL provider to implement the IQueryable interface, used to query relational data storage. C # and Visual Basic compiler will check for the compilation of such data sources for the code, the code will be generated at run time an expression tree. Then, the query provider can traverse an expression tree data structure and convert it to fit the data source query language.

LINQ expression tree is used in that type assigned to Lambda Expression of variable expression. Can also be used to create dynamic LINQ query.

System.Linq.Expressions namespace provides for manually generated expression tree API. Expression class contains a particular type of expression to create tree nodes static factory method, for example, ParameterExpression (that a named parameter expression) or MethodCallExpression (represents a method call). Compiler generates an expression tree is always the root of the node in the type of Expression, which TDelegate that contains up to five input parameters of any TDelegate commission; that is, its root is that a lambda expression.

The following examples describe how to use the expression tree to create a dynamic LINQ query.

Select advanced features of LINQ

The following example shows how to use IQueryable expression trees based on the data source to construct a dynamic query, the query of each customer's ContactName, and methods for their use GetCommand generated SQL statement.



/ / Based on the data source to construct a query IQueryable
IQueryable custs = db.Customers;
/ / Set up an expression tree to create a parameter
ParameterExpressionparam = Expression.Parameter (typeof (Customer), "c");
/ / Set expression tree
c.ContactNameExpressionselector = Expression.Property (param, typeof (Customer).
GetProperty ("ContactName"));
ExpressionExpressionpred = Expression.Lambda (selector, param);
/ / Set up the expression tree: Select (c => c.ContactName) ExpressionExpressionexpr = Expression.Call
(Typeof (Queryable), "Select", newType [] (typeof (Customer), typeof (string)),
Expression.Constant (custs), pred);
/ / Use the expression tree to generate dynamic query IQueryable query = db.Customers.AsQueryable ()
. Provider.CreateQuery (expr);
/ / Use method for SQL statements GetCommand
System.Data.Common.DbCommandcmd = db.GetCommand (query);
Console.WriteLine (cmd.CommandText);





Recommended links:



Hot Astrology Or Biorhythms Or Mystic



DivX To IPod



TS TO MKV



Jsp, servlet talk about Chinese issues



3GP to WMV



The biggest killer of retail sales



Using a password reset DISK recover lost XP passwords



Thunder turned to commercial content channels: forced out of the exploration



PICKED Install And Setup



DDOS DDOS tracking the INTRODUCTION and



Internal control: harm than good, or at every step



C2C staged "Three Kingdoms"



Handy program 3



Debugging Report



Understanding Of EIP



A large number of international giants fight back to the Chinese. CN domain names



Report Basic Or VB Or VB DOTNET