Click here to Skip to main content
13,510,146 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 26 Jul 2011

IQueryable vs. IEnumerable in terms of LINQ to SQL queries

, 26 Jul 2011
Rate this:
Please Sign up or sign in to vote.
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.

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.


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

Microsoft C# MVP (12-13)

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 -
My CV :-


You may also be interested in...


Comments and Discussions

GeneralMy vote of 3 Pin
sarathkumarnallathambi12-Feb-14 20:09
membersarathkumarnallathambi12-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
memberjadavparesh0623-Jul-13 18:55 
GeneralMy vote of 5 Pin
maheshbisht8-Oct-12 8:53
membermaheshbisht8-Oct-12 8:53 
BugWrong info provided Pin
uandme7719-Jun-12 2:41
memberuandme7719-Jun-12 2:41 
GeneralMy vote of 5 Pin
Ajanta Mittal21-Feb-12 0:16
memberAjanta 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.

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