Latency loading of Linq

Source: Internet
Author: User

Latency loading of Linq
Directory

Preface

Series of articles

Delayed Loading

Summary

Preface

In the previous article, I introduced several common keywords in linq and listed several examples. I have a preliminary understanding of how to use linq. As mentioned in the previous article, there is a requirement to be able to use linq: to implement IEnumerable <T> generic interfaces, or to be compatible with types (you can convert them using Cast methods, such as ArrayList ).

Series of articles

A preliminary understanding of Lambda expressions in Linq

Lambda of Linq (advanced tutorial)

Implicit type, automatic attribute, initializer, and Anonymous class of Linq

Extended method of Linq

First Appearance of Expression of Linq

Expression IN Linq (advanced tutorial)

Expression of Linq (common Expression types)

Common keywords of Linq

Delayed Loading

Latency loading is supported in many orm frameworks. What is latency loading? To put it simply, you need to query it again when you need it. If you don't need it, you don't need it.

The execution result of the Linq query is IEnumerable <T> type. For IEnumerable <T>, inside, C # uses the yield keyword to implement the iterator to achieve delayed loading. In this way, the Linq query is executed only when necessary.

The following is an example.

1 namespace Wolfy. linqLazyLoad 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 List <Person> persons = new List <Person> () {8 new Person () {ID = 1, Name = "wolfy1", Age = 1}, 9 new Person () {ID = 2, Name = "wolfy2", Age = 2 }, 10 new Person () {ID = 3, Name = "wolfy3", Age = 3}, 11 new Person () {ID = 4, Name = "wolfy4 ", age = 4}, 12 new Person () {ID = 5, Name = "wolfy5", Age = 5}, 13 new Person () {ID = 6, name = "wolfy6", Age = 6} 14}; 15 // use linq to query 16 var query = from p in persons17. orderByDescending (p => p. age) 18 select new {p. ID, p. name, p. age}; 19 // if the response is delayed, the output result should be modified (delayed loading indicates that no data is actually loaded in the query) 20 // If linq loads the data immediately, the query is equivalent to a temporary buffer. The data already exists in the query, even if one of the modifications to persons does not affect the data in the query. 21 persons [2] = new Person () {ID = 7, Name = "zhangsan", Age = 7}; 22 foreach (var item in query) 23 {24 Console. writeLine (item. toString (); 25} 26 Console. read (); 27} 28} 29 class Person30 {31 public int ID {set; get;} 32 public string Name {set; get;} 33 public int Age {set; get;} 34 public override string ToString () 35 {36 return ID + "" + Name + "" + Age; 37} 38} 39}

The example is very simple. It is output in descending order of age through the linq query.

Let's take a look at the output result.

You may not be very clear about this.

Next, let's take another example of how to load the data immediately using linq.

1 static void Main (string [] args) 2 {3 List <Person> persons = new List <Person> () {4 new Person () {ID = 1, name = "wolfy1", Age = 1}, 5 new Person () {ID = 2, Name = "wolfy2", Age = 2}, 6 new Person () {ID = 3, Name = "wolfy3", Age = 3}, 7 new Person () {ID = 4, Name = "wolfy4", Age = 4 }, 8 new Person () {ID = 5, Name = "wolfy5", Age = 5}, 9 new Person () {ID = 6, Name = "wolfy6 ", age = 6} 10}; 11 // use aggregate function total Age 12 var result = (from p in persons1 3 select p. age) 14. sum (); 15 // if the response is delayed, the output result should be modified (delayed loading indicates that no data is actually loaded in the query at this time) 16 // If linq loads the data immediately, the query is equivalent to a temporary buffer. The data already exists in the query, even if one of the modifications to persons does not affect the data in the query. 17 persons [2] = new Person () {ID = 7, Name = "zhangsan", Age = 7}; 18 Console. writeLine ("Sum" + result); 19 Console. read (); 20}

Output result

21 = 1 + 2 + 3 + 4 + 5 + 6. This also illustrates a problem. In linq, Some Aggregate functions such as Sum and averaging will affect the delayed loading feature of linq.

The first example above is delayed loading. Data is not loaded in the query, and then you modify the value of persons [2]. When you output each value in the query, in this case, the data is actually loaded, and the value of persons [2] has changed, so the latest persons [2] will be output.

In the second example, why does an aggregate function affect the delayed loading feature? For example, in this example, the sum operation requires all values, therefore, you need to query the value before sum can be obtained. At this time, the result is saved in the result. Even if you modify the value of persons [2] below, it will be useless.

Summary

1. latency loading is available in linq.

2. When an aggregate function is used in linq, the delayed loading feature becomes invalid.

The above example is a bit difficult. You need to understand it for various reasons.

Think: why can the yield keyword realize the delayed loading feature? (Looking for a lot of information, unsuccessful)

References

Http://kb.cnblogs.com/page/100043/

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.