LINQ-Deferred execution

Source: Internet
Author: User

/* Author: Jiangong SUN */Deferred Execution is used in LINQ, and it means the query is not executed when declaring it; but when calling it. if you return IQueryable or IEnumerable in your query, the query will not be executed immediately, it will be executed only when you iterate its elements. but if you return operations on IQueryable or IEnumerable like. toList (), Count () etc, it will be executed im Mediately. let's see some examples: public void EnumerableCustomers () {int id = 10; // differed execution: when query returns IEnumerable or IQueryable var query = from c in Context. customers where c. customerId> id select c; id = 20; // this value is finally used because it's the latest value Console. writeLine ("Query result:"); // return customer names whose id is larger than 20 foreach (var custom Er in query) Console. writeLine (customer. customerName);} private void EnumerableCustomers2 () {int id2 = 10; // immediate execution: when query doesn't return IEnumerable or IQueryable var query2 = (from c in Context. customers where c. customerId> id2 select c ). toList (); id2 = 20; // this value is not used in this case Console. writeLine ("Query2 result:"); // return customer names whose id is large th An 10 foreach (var customer in query2) Console. WriteLine (customer. CustomerName);} I hope this article can do help to you! Enjoy coding! References: http://blogs.msdn.com/ B /charlie/archive/2007/12/09/deferred-execution.aspxearly binding, late binding: http://blogs.msdn.com/ B /cburrows/archive/2008/10/27/c-dynamic.aspx

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.