程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> LINQ - Deferred execution

LINQ - Deferred execution

編輯:C#入門知識

/* 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 immediately.   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 customer 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 than 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.aspx early binding, late binding: http://blogs.msdn.com/b/cburrows/archive/2008/10/27/c-dynamic.aspx

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved