Bernie Cook's Blog

Azure, C#, .NET, Architecture & Related Tech News

IQueryable, IEnumerable & List – What’s the Difference?

Leave a comment

Back when .NET 2 was released in 2006 Lists appeared and I remember having one of those “OMG this is fantastic moment for coding“. You could immediately see the benefits – how they’d reduce the amount of code you had to write and increase readability. Prior to that in .NET 1.1 there was IEnumerable, which I can’ t say was something I made a lot direct use of, with emphasis on the word “direct”. I utilised a number of classes that implemented IEnumerable (SortedList, Hashtable, List, etc.) but, unlike today, it wasn’t a class that I worked with directly. Then a few years later .NET 3.5 was released and IQueryable was brought into the mix and suddenly I started having to think a touch more about which one was the “right” one to use when it came to manipulating a collection of objects.

I work with object collections a lot, whether it be generating them in code, loading and manipulating them from a database, generating them from input etc. and it’s always important for me to know the best way of manipulating them – hopefully this applies to you as well, otherwise feel free to call me narcissistic :). Microsoft makes all these great coding tools available to the developer, and as a developer you want to use the right tool for the job … right? :). So I thought I’d write a blog about this dated, but still commonly asked question.

What’s the Difference?

Here’s the simple way to think of the three classes/interfaces – and I’ve purposefully removed a lot of technical jargon, i.e. no talk of Expression objects or Func object:

  1. List – A strongly typed list of in-memory objects that can be manipulated in just about every way possible. You can do a lot with a List of objects; sorting, counting, does or doesn’t contain, finding, removing etc. – it’s like the Swiss Army knife of collections
  2. IEnumerable – Ignoring the fact that it’s an interface for a moment, it’s simpler version of a List – it works with objects in-memory but it’s forward moving only and doesn’t expose a lot of members/methods that List does.
  3. IQueryable – First off it’s important to note that this interface extends IEnumerable, and there are 3 key differences; (i) it points to a query provider (database, web services, etc.), (ii) it uses a query expression to correctly translate what it’s asking for from the data source in question, and (iii) it doesn’t work with objects in-memory. IQueryable is more commonly found when working with LINQ to SQL or Entity Framework.

If I’m writing a new class that will require some form of collection request and manipulation I always ask myself these two questions to help me pinpoint the appropriate class/interface:

  1. Am I interfacing with a data source, or just working with object in-memory?
  2. What do I want to do with the collection once I have it?

The answers usually provide me with the right class/interface.

A Bit More…

So List and IEnumerable are similar in that they:

  1. They are not a query, but a collection of objects
  2. Allow you to work with objects in-memory
IEnumerable is like IQueryable in that:
  1. IQueryable extends IEnumerable
  2. In the case of LINQ you can keep extending it and execute the LINQ when you’re ready

Also keep an eye on performance and compare the two queries using the appropriate tools if you want to delve deeper into what’s going on under the hood.

Good luck.

Author: Bernie

I currently live and work in the UK, just outside of London. I've been working in IT for 15+ years and in that time have solved many technical problems via blogs, forums, tutorials etc. and feel like I should be giving something back. This blog post is my way of contributing and I hope it proves just as useful to others as their contributions have to me.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s