Tuesday, October 19, 2010

LINQ subquery and delay the implementation of programming examples



Many friends and delay the implementation of LINQ subquery usage is not very clear, the following will give you an example to show the relevant LINQ subquery usage.

LINQ subquery

LINQ subquery is a query that contains another query Lambda expressions. The following example uses a LINQ query against the basketball star son's last name sort:



string [] players = ("Tim Ducan", "Lebrom James", "Kobe Byrant");
IEnumerable q = players.OrderBy (m => m.Split (). Last ()); In this one, Last is a LINQ sub query, q represents an external inquiry.

Sub-query in LINQ, Lambda expressions you can use any possible right of expression of C # syntax. LINQ subquery is a simple C # expression, which means that all applicable rules in LINQ sub query expression can be derived to Lambda ceremony.

The following query to get a character array of length equal to all meet the minimum length of the character sequence:



string [] names = ("James", "Jack", "Landy", "C.Y", "Jay");
IEnumerable q = names
. Where (n => n.Length ==
names.OrderBy (n2 => n2.Length)
. Select (n2 => n2.Length). First ()
);
foreach (var s in q)
(
Console.WriteLine (s); / / C.Y, Jay
) Sub for LINQ queries, can refer to the Lambda parameters or external iteration variables (in the compound query). Such as the above example, if the expression used to OrderBy (n => n.Length) rather than n2 will be used if an error message:



IEnumerable q =
from n in names
where n.Length ==
(From n2 in names
orderby n2.Length
select n2.Length). First ()
select n; for this example, we can see the corresponding complex queries written:



IEnumerable q =
from n in names
where n.Length ==
names.OrderBy (n2 => n2.Length). First (). Length
select n; external iteration variables n in the context of LINQ subquery is visible, we can not reuse it as the LINQ subquery within the iteration variable.

LINQ subquery will be executed corresponding to the time of Lambda expressions to implement, their implementation depends on the outer query, it can be said from the outside to inside to deal with. Local inquiries in full compliance with this model, but interpreted query (such as LINQ to SQL) is just a concept to follow it.

Inquiries before we can use a more concise wording:



IEnumerable q =
from n in names
where n.Length == names.Min (n2 => n2.Length)
select n; If you use the Min aggregate functions can be further simplified:

IEnumerable q = from n in names where n.Length == names.Min (n2 => n2.Length) select n;

In fact, as n2.Length loop when the external query recalculated every time, which in some cases may lead to efficiency, to avoid this problem, we can separate the LINQ subquery:



int len = names.Min (n => n.Length);

IEnumerable query = from n in names
where n.Length == len
select n; sub-query and delay the implementation of

Sub-query in LINQ to return a single element or aggregate class of operators, such as first or Count, and not the immediate implementation of mandatory external inquiry, also said an external inquiry still has the capacity of the delay. This is because LINQ sub query is being indirectly call - if the local inquiry, is through a proxy (delegate), if the contact was through the interpretative expression tree (expression tree).

An interesting phenomenon is that when your LINQ sub query that contains a Select expression, if one is a local query, you are actually their divergence into a sequence of queries - and each one has its ability to delay implementation. That the effect transparent, because it can significantly improve efficiency.







Recommended links:



Export and the introduction Of the registry



DivX to IPod



Jobs sick industry CONCERNS raised



My favorite Hobby



INFOMATION Gallery And Cataloging Tools



How to PREVENT closure of factory promotions?



Beijing TV (radio and TELEVISION media)



OGM To AVI



SYBASE Services



comparison Project Management



Students this year, real SALARY levels Jiemi fresh



ASF TO MPG



Compare Vertical Market Apps



Out errors to make your writing more efficient CSS



bridge router functions of the vlan classification



No comments:

Post a Comment