Click here to Skip to main content
Click here to Skip to main content

IQueryable vs. IEnumerable in terms of LINQ to SQL queries

By , 26 Jul 2011
 

A few days ago, I was working on a project which has LINQ to SQL as the database layer. I got a requirement to get the first name of all employees whose designation starts with "soft".

I fired the below query and got the result in an IEnumerable variable.

IEnumerable<employee> emp = 
         dc.Employees.Where(x => x.Desc.StartsWith("soft"));
emp = emp.Take(1);

But later on, I found it was taking too much time to get the count.

So to try something else, I uses an IQueryable to store the result and to get the count.

IQueryable<employee> emplist = 
         dc.Employees.Where(x => x.Desc.StartsWith("soft"));
emplist = emplist.Take(1);

After using IQueryable, I found that I got the result faster than the last time.

To find out what was causing this, I used SQL Profile to find out the SQL query fired by the code.

The first block of code fired the following query, i.e., the one which uses IEnumrable to store the output of the LINQ query.

SELECT [t0].[Id], [t0].[Name], [t0].[Address], [t0].[Desc] AS [Desc]
FROM [dbo].[Employee] AS [t0]
WHERE [t0].[Desc] LIKE @p0

The second block of code fired the following query, i.e., which uses IQuerable to store the output of the LINQ query.

SELECT TOP (1) [t0].[Id], [t0].[Name], [t0].[Address], [t0].[Desc] AS [Desc]
FROM [dbo].[Employee] AS [t0]
WHERE [t0].[Desc] LIKE @p0

The major difference between the queries is the first one doesn't contain the TOP clause to get the first record but the second one makes use of TOP to get the first record.

When I explored further I found following difference between them

The major difference is that IEnumerable will enumerate all elements, while IQueryable will enumerate elements (or even do other things) based on a query. In the case of the IQueryable, the LINQ query gets used by IQueryProvider which must be interpreted or compiled in order to get the result. I.e., the extension methods defined for IQueryable take Expression objects instead of Func objects (which is what IEnumerable uses), meaning the delegate it receives is an expression tree instead of a method to invoke.

IEnumerable is great for working with in-memory collections, but IQueryable<t> allows for a remote data source, like a database or web service.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India
Member

Microsoft C# MVP

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermaheshbisht8 Oct '12 - 8:53 
BugWrong info providedmemberuandme7719 Jun '12 - 2:41 
GeneralMy vote of 5memberAjanta Mittal21 Feb '12 - 0:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 26 Jul 2011
Article Copyright 2011 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid