Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the delay execution of Linq

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand the implementation of Linq delay". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the implementation of Linq delay.

An important feature of most of Linq's query operators is that they are not executed immediately at build time, but when enumerations are executed, in other words, when enumerated variables call MoveNext.

After the query is built, additional numbers inserted into the list are also included in the structure, because filtering or sorting operations are not performed until foreach runs, which is called deferred execution or deferred computation. all standard query operators are deferred execution, but some operators do not support deferred execution mechanism, but execute immediately, such as Count, ToAarry, toLookup, and so on.

Linq deferred execution also has a negative effect. If the lambda expression of the query refers to a local variable, then these will be subject to external semantic constraints.

This becomes a trap when building a query in a foreach loop, such as assuming that you want to delete all vowels in a string. Although inefficient, the correct results can be obtained as shown below:

IEnumberable qurey = "Not what you might expect"; queryquery = query.Where (c = > cantilever rooma'); queryquery = query.Where (c = > cantilever roome'); queryquery = query.Where (c = > cymbal roomi'); queryquery = query.Where (c = > cymbal roomo'); queryquery = query.Where (c = > cymbal roomu'); foreach (char c in query) Console.Write (c); / / Nt wht y mght xpct

The above code can get the correct result, so now if you rewrite this code with a foreach loop, you can get the correct result:

IEnumerable query = "Not what you might expect"; foreach (cha vowel in "aeiou") qwuer = query.Where (c = > cymbal vowel); foreach (char c in query) Console.Write (c); / / Not what yo might expect / / only removes' uplink, because the compiler translates the foreach loop into the following code: IEnumerable vowels= "aeiou"; Iemuberator rator=vowels.GetEnumerator (); char vowel; while (rator.MoveNext ()) {vowel = rator.Current Queryquery = query.Where (c = > cantilever vowel);}

Because the vowel variable is declared outside the loop, and the same variable is repeatedly declared to update, each lambda expression gets the same vowel. When you enumerate the query later, all lambda expressions refer to the current value of the variable, namely'u'. To solve this problem, you must assign a loop variable to a variable declared in another repeated loop block of code:

IEnumberable qurey = "Not what you might expect"; foreach (char vowel in "aeiou") {char temp = vowel; queryquery = query.Where (c = > clocked delay temp);} at this point, I believe you have a better understanding of "how to understand Linq delay execution", so you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report