CodeToClarity Logo
Published on ·4 min read·C#

IEnumerable vs IQueryable in C#: Explained with Examples (Beginner’s Guide)

Kishan KumarKishan Kumar

Learn the real difference between IEnumerable and IQueryable in C#. Understand how they work, when to use which, and how they impact performance with practical examples.

When working with data in modern .NET applications, developers often face a common question:

Should I use IEnumerable or IQueryable?

Both are powerful interfaces that let you query and manipulate data, but they work very differently behind the scenes.
Choosing the right one can make a big difference in your app’s performance, memory usage, and scalability.

In this guide, we’ll break everything down step by step, in simple terms, so even if you’re just starting with .NET, you’ll understand exactly how and when to use each.


📘 What is IEnumerable?

IEnumerable is an interface that represents a sequence of objects that can be iterated (looped) over.
It belongs to the System.Collections namespace.

You can think of it as a tool to work with data already loaded into memory, such as a list or an array.

🔹 Key Points

  • Works in-memory (after the data is loaded).
  • Supports deferred execution for methods like Where, Select, etc.
  • Cannot translate queries to SQL when used with Entity Framework.
  • Perfect for small, in-memory collections like List<T> or Array.

💻 Example: Using IEnumerable

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };

        IEnumerable<int> evenNumbers = numbers.Where(n => n % 2 == 0);

        foreach (var num in evenNumbers)
        {
            Console.WriteLine(num);
        }
    }
}

🟢 Here, all data (numbers) is already in memory.
The filtering (n % 2 == 0) happens in-memory, not in a database.


🌐 What is IQueryable?

IQueryable extends IEnumerable and is designed for querying remote data sources, such as databases.
It’s part of the System.Linq namespace.

In short: it lets you write LINQ queries that get translated into SQL and executed on the database server instead of in your app.

🔹 Key Points

  • Supports deferred execution.
  • Can translate queries into SQL (works great with Entity Framework).
  • Executes filters, sorting, and joins on the database, reducing memory usage.
  • Ideal for large datasets and remote queries.

💻 Example: Using IQueryable

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;

class Program
{
    static void Main()
    {
        using var context = new AppDbContext();

        IQueryable<User> adultUsers = context.Users
                                             .Where(u => u.Age >= 18);

        foreach (var user in adultUsers)
        {
            Console.WriteLine($"{user.Name} - {user.Age}");
        }
    }
}

🟢 In this example, the WHERE condition runs directly in the database.
Only the filtered records are fetched, saving time and memory.


⚖️ IEnumerable vs IQueryable: Key Differences

FeatureIEnumerableIQueryable
NamespaceSystem.CollectionsSystem.Linq
Execution LocationIn-memoryDatabase (remote)
Query TranslationNo SQL translationTranslates to SQL
Deferred ExecutionYesYes
Best ForSmall in-memory collectionsLarge datasets or databases
PerformanceSlower for large dataFaster due to server-side filtering

💡 When Should You Use Which?

Use IEnumerable when:

  • You’re working with in-memory data (like lists, arrays, or results already fetched).
  • You want simple iterations and lightweight operations.

Use IQueryable when:

  • You’re working with databases or remote data sources.
  • You want filtering, sorting, or paging to happen on the server for better performance.

⚠️ Pro Tip:
Avoid calling .ToList() or .ToArray() too early on an IQueryable object.
Doing so forces it to load all the data into memory, killing the performance benefits.


🛠️ Real-World Example

Imagine you have a database with millions of users, and you want users older than 30.

❌ Using IEnumerable

var users = context.Users.ToList(); // Loads all users in memory
var result = users.Where(u => u.Age > 30);

👉 This loads every record into memory first, which is slow and wasteful.

✅ Using IQueryable

var result = context.Users.Where(u => u.Age > 30);

👉 This generates a SQL query like:

SELECT * FROM Users WHERE Age > 30

and only retrieves the needed users, which is much faster and more efficient.


🧾 Summary

FeatureIEnumerableIQueryable
ExecutionIn-memoryServer-side (Database)
Use CaseSmall datasetsLarge datasets
PerformanceSlowerFaster
SQL Translation❌ No✅ Yes

In short:

  • IEnumerable → Great for simple, local collections.
  • IQueryable → Perfect for database or large dataset queries.

🚀 Final Thoughts

Understanding the difference between IEnumerable and IQueryable is key to writing efficient and scalable .NET applications.

Next time you write a LINQ query, pause and think:

Am I working with data that’s already in memory or still in the database?

That single choice can make a huge performance difference.


Thanks for reading! 🙌
If you found this guide helpful, don’t forget to share it with your developer friends or team!

Kishan Kumar

Kishan Kumar

Software Engineer / Tech Blogger

LinkedInConnect

A passionate software engineer with experience in building scalable web applications and sharing knowledge through technical writing. Dedicated to continuous learning and community contribution.