Friday, October 8, 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:



To prevent the most effective way to Trojan



Chemistry teachers and multimedia courseware



AVI to iPod



Report Basic Or VB Or VB DotNet



VBScript Length property



3GP to MPEG



for you ADVENTURE And Roleplay



"Want to" match "can do"



get Accurate data with data cleansing



MKV to VOB



Single-core Conroe Two Series Were Named Pentium, Celeron



Operators to remind: strange sounds for calls do not hang back On



Of C # (11) Enumeration (Enums)



New Vertical Market APPS



No trace of her it just so to find the ORIGINAL 1000 Baidu



Specialist Pager Tools



No comments:

Post a Comment