LINQ Learning (7) deferred execution & query non-generic collections

Source: Internet
Author: User

This article describes the deferred execution of LINQ and Query Non-generic collections.

Deferred execution

From the perspective of LINQ query process, there are new types, collection construction, release, feeling inefficient, but the delayed execution mechanism of LINQ will greatly improve the general operation of loading the entire data source caused by the inefficient situation.

Example:

static void Main()
{
  int[] collection = { 10, 11, 12, 13, 14 };
  Console.WriteLine("Begin query collection:" + DateTime.Now);
  var result = from i in collection
         select DoubleInt(i);
  Console.WriteLine("End query collection & Begin output:" + DateTime.Now);
  foreach (var i in result)
  {
    Console.WriteLine("Result:" + i + " Time:" + DateTime.Now.ToString());
  }
  Console.WriteLine("End output:" + DateTime.Now);
}
static int DoubleInt(int i)
{
  Console.WriteLine("The parameter is:" + i);
  Thread.Sleep(1000);
  return i * 2;
}

The output is:// Begin query collection:2008-9-12 22:41:50
// End query collection & Begin output:2008-9-12 22:41:50
// The parameter is:10
// Result:20 Time:2008-9-12 22:41:51
// The parameter is:11
// Result:22 Time:2008-9-12 22:41:52
// The parameter is:12
// Result:24 Time:2008-9-12 22:41:53
// The parameter is:13
// Result:26 Time:2008-9-12 22:41:54
// The parameter is:14
// Result:28 Time:2008-9-12 22:41:55
// End output:2008-9-12 22:41:55

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.