Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / C#

IQueryable vs. IEnumerable in terms of LINQ to SQL queries

Rate me:
Please Sign up or sign in to vote.
4.70/5 (22 votes)
26 Jul 2011CPOL2 min read 80.5K   25   6
Difference between IQueryable and IEnumerable in terms of LINQ to SQL queries.

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.

C#
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.

C#
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.

C#
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.

C#
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)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin 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:



Comments and Discussions

 
GeneralMy vote of 3 Pin
Sarathkumar Nallathamby12-Feb-14 20:09
professionalSarathkumar Nallathamby12-Feb-14 20:09 
GeneralMy vote of 5 Pin
Pratik Bhuva18-Nov-13 20:02
professionalPratik Bhuva18-Nov-13 20:02 
GeneralMy vote of 4 Pin
jadavparesh0623-Jul-13 18:55
jadavparesh0623-Jul-13 18:55 
GeneralMy vote of 5 Pin
maheshbisht8-Oct-12 8:53
maheshbisht8-Oct-12 8:53 
BugWrong info provided Pin
uandme7719-Jun-12 2:41
uandme7719-Jun-12 2:41 
GeneralMy vote of 5 Pin
Ajanta Mittal21-Feb-12 0:16
Ajanta Mittal21-Feb-12 0:16 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.