{"id":396,"date":"2014-09-25T11:46:03","date_gmt":"2014-09-25T11:46:03","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=396"},"modified":"2014-09-25T11:46:03","modified_gmt":"2014-09-25T11:46:03","slug":"ienumerable-vs-iqueryable","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=396","title":{"rendered":"IEnumerable VS IQueryable"},"content":{"rendered":"<h2>IEnumerable<\/h2>\n<ol>\n<li>IEnumerable exists in System.Collections Namespace.<\/li>\n<li>IEnumerable can move forward only over a collection, it can\u2019t move backward and between the items.<\/li>\n<li>IEnumerable is best to query data from in-memory collections like List, Array etc.<\/li>\n<li>While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.<\/li>\n<li>IEnumerable is suitable for LINQ to Object and LINQ to XML queries.<\/li>\n<li>IEnumerable supports deferred execution.<\/li>\n<li>IEnumerable doesn\u2019t supports custom query.<\/li>\n<li>IEnumerable doesn\u2019t support lazy loading. Hence not suitable for paging like scenarios.<\/li>\n<li>Extension methods supports by IEnumerable takes functional objects.<\/li>\n<\/ol>\n<p>IEnumerable\u00a0is the base interface for all non-generic collections that can be enumerated. For the generic version of this interface see\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/9eekhta0(v=vs.110).aspx\">System.Collections.Generic.IEnumerable&lt;T&gt;<\/a>.\u00a0IEnumerable\u00a0contains a single method,<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerable.getenumerator(v=vs.110).aspx\">GetEnumerator<\/a>, which returns an\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator(v=vs.110).aspx\">IEnumerator<\/a>.\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator(v=vs.110).aspx\">IEnumerator<\/a>\u00a0provides the ability to iterate through the collection by exposing a\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator.current(v=vs.110).aspx\">Current<\/a>\u00a0property and\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator.movenext(v=vs.110).aspx\">MoveNext<\/a>\u00a0and\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator.reset(v=vs.110).aspx\">Reset<\/a>\u00a0methods.<\/p>\n<p>It is a best practice to implement\u00a0IEnumerable\u00a0and\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator(v=vs.110).aspx\">IEnumerator<\/a>\u00a0on your collection classes to enable the\u00a0foreach\u00a0(For Each\u00a0in Visual Basic) syntax, however implementing\u00a0IEnumerable\u00a0is not required. If your collection does not implement\u00a0IEnumerable, you must still follow the iterator pattern to support this syntax by providing a\u00a0GetEnumerator\u00a0method that returns an interface, class or struct. When using Visual Basic, you must provide an\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator(v=vs.110).aspx\">IEnumerator<\/a>\u00a0implementation, which is returned by\u00a0GetEnumerator. When developing with C# you must provide a class that contains a\u00a0Current\u00a0property, and\u00a0MoveNext\u00a0and\u00a0Reset\u00a0methods as described by<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator(v=vs.110).aspx\">IEnumerator<\/a>, but the class does not have to implement\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerator(v=vs.110).aspx\">IEnumerator<\/a>.<\/p>\n<h2>IEnumerable Example<\/h2>\n<ol>\n<li>MyDataContext dc = new MyDataContext ();<\/li>\n<li>IEnumerable&lt;Employee&gt; list = dc.Employees.Where(p =&gt; p.Name.StartsWith(&#8220;S&#8221;));<\/li>\n<li>list = list.Take&lt;Employee&gt;(10);<\/li>\n<\/ol>\n<h3>Generated SQL statements of above query will be :<\/h3>\n<ol>\n<li>SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]<\/li>\n<li>WHERE [t0].[EmpName] LIKE @p0<\/li>\n<\/ol>\n<p>Notice that in this query\u00a0<b>&#8220;top 10&#8221;<\/b>\u00a0is missing since IEnumerable filters records on client side<\/p>\n<h2>IQueryable<\/h2>\n<ol>\n<li>IQueryable exists in System.Linq Namespace.<\/li>\n<li>IQueryable can move forward only over a collection, it can\u2019t move backward and between the items.<\/li>\n<li>IQueryable is best to query data from out-memory (like remote database, service) collections.<\/li>\n<li>While query data from database, IQueryable execute select query on server side with all filters.<\/li>\n<li>IQueryable is suitable for LINQ to SQL queries.<\/li>\n<li>IQueryable supports deferred execution.<\/li>\n<li>IQueryable supports custom query using CreateQuery and Execute methods.<\/li>\n<li>IQueryable support lazy loading. Hence it is suitable for paging like scenarios.<\/li>\n<li>Extension methods supports by IQueryable takes expression objects means expression tree.<\/li>\n<\/ol>\n<p>The\u00a0IQueryable\u00a0interface inherits the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.ienumerable(v=vs.100).ASPX\">IEnumerable<\/a>\u00a0interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an\u00a0IQueryable\u00a0object to be executed. The definition of &#8220;executing an expression tree&#8221; is specific to a query provider. For example, it may involve translating the expression tree to an appropriate query language for the underlying data source. Queries that do not return enumerable results are executed when the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.linq.iqueryprovider.execute(v=vs.100).ASPX\">Execute<\/a>\u00a0method is called.<\/p>\n<h2>IQueryable Example<\/h2>\n<ol>\n<li>MyDataContext dc = new MyDataContext ();<\/li>\n<li>IQueryable&lt;Employee&gt; list = dc.Employees.Where(p =&gt; p.Name.StartsWith(&#8220;S&#8221;));<\/li>\n<li>list = list.Take&lt;Employee&gt;(10);<\/li>\n<\/ol>\n<h3>Generated SQL statements of above query will be :<\/h3>\n<ol>\n<li>SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]<\/li>\n<li>WHERE [t0].[EmpName] LIKE @p0<\/li>\n<\/ol>\n<p>Notice that in this query\u00a0<b>&#8220;top 10&#8221;<\/b>\u00a0is exist since IQueryable executes query in SQL server with all filters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">IEnumerable IEnumerable exists in System.Collections Namespace. IEnumerable can move forward only over a collection, it can\u2019t move backward and between the items. IEnumerable is best to query data from in-memory collections like List, Array etc. While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. IEnumerable is suitable&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=396\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7,11],"tags":[],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-c-vb","category-c","category-oop","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=396"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}