Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is IEnumerable?
2.1.
IEnumerable Example
2.2.
C#
2.3.
Explanation
3.
What is IQueryable?
3.1.
IQueryable Example
3.2.
C#
3.3.
Explanation
4.
Difference between IEnumerable and IQueryable
5.
Which is better: IEnumerable or IQueryable?
6.
Frequently Asked Questions
6.1.
Which is faster IEnumerable or IQueryable?
6.2.
What is the difference between IEnumerable & IQueryable?
6.3.
What is the difference between IEnumerable and IQueryable repository?
6.4.
What is difference between IEnumerable and List in C#?
6.5.
What is the difference between IEnumerator and IEnumerable?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Difference between IEnumerable vs IQueryable

Introduction

IEnumerable and IQueryable are both interfaces in .NET that represent collections of objects, but they have different purposes and behaviors. IEnumerable is suitable for in-memory collections, and the entire data is loaded into memory at once. IQueryable is designed for querying data from a remote data source, and the actual execution of the query is deferred until the data is explicitly requested.

Difference between IEnumerable vs IQueryable

We will also learn about representing the IEnumerable and IQueryable in C# language and queryable in SQL commands.

You might be wondering what IEnumerable and IQueryable are used for. You should know that they are generally used for Data manipulation, such as ordering, grouping, and filtering. They are also used to hold the collection of data.

What is IEnumerable?

The IEnumerable interface offers a single method, GetEnumerator(), which returns the IEnumerator interface. It may be found in the system collections. It iterates over the classes using the for-each loop.

In C#, IEnumerable is an interface that allows iteration across non-generic collections. It is included with the System. The namespace for collections.

In general, the IEnumerable interface represents an object that can be enumerated and serves as the foundation interface for any non-generic collections that can be enumerated. The following code will be included in the IEnumerable interface class.

The type of object in the enumerator is specified by "T" in IEnumerable <T>.

getEnumerator() returns an enumerator that loops across the collection.

IEnumerable Example

Here's a simple example using IEnumerable in C#. In this case, we will demonstrate iterating over a collection of integers:

  • C#

C#

using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
// Creating an IEnumerable collection (a List in this example)
IEnumerable<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Iterating over the collection using foreach
foreach (var number in numbers)
{
Console.WriteLine(number);
}
}
}

Output

output

Explanation

In this example, numbers is an IEnumerable<int> representing a list of integers. The foreach loop is used to iterate through each element in the collection, printing the numbers to the console. This showcases the basic usage of IEnumerable for iterating over an in-memory collection.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is IQueryable?

IQueryable again is an interface that inherits the IEnumerable interface through which it can represent the query. It used expression trees to translate LINQ queries into the query language that the data was input into the database. 

The T in the IQueryable <T> is the data source type.

IQueryable Example

Here's an example using IQueryable in C#. In this example, we will use a simple scenario of querying a collection of numbers from a database. In this example, we are assuming that there is a DbContext with a Numbers DbSet. 

  • C#

C#

using System;
using System.Linq;

// Assume there is a DbContext with a Numbers DbSet
class Program
{
static void Main()
{
// Creating an IQueryable collection from a database context
IQueryable<int> queryableNumbers = dbContext.Numbers;

// Building a query (filtering numbers greater than 3)
var result = queryableNumbers.Where(n => n > 3).ToList();

// Displaying the result
foreach (var number in result)
{
Console.WriteLine(number);
}
}
}

Explanation

In this example, queryableNumbers is an IQueryable<int> representing a collection of numbers from a database. The Where clause is used to filter numbers greater than 3, and ToList() triggers the execution of the query against the database. Finally, the result is iterated over using foreach to print the filtered numbers to the console. This illustrates the use of IQueryable for building and executing queries against a remote data source.

Difference between IEnumerable and IQueryable

To simplify IEnumerable vs IQueryable, you can reference the table below.

IEnumerable

IQueryable

It is in the namespace System.Collections. It is located in the System.LINQ namespace.
It does not allow for Lazy-Loading. It supports Lazy-Loading.
LINQ to Object and LINQ to XML queries are the most common Mainly used for LINQ to SQL and LINQ to entities.
It is not appropriate for paging-like situations. It is suitable for paging-like situations.
It is ideal for query data from in-memory collections like Array. It is suitable for querying data from out-memory like remote databases.
Custom queries are not supported. It supports custom queries. You can create it using CreateQuery and execute it.

Which is better: IEnumerable or IQueryable?

Based on IEnumerable vs IQueryable, we can conclude that if you are solely working with an in-memory database, IEnumerable is the best option. However, if you need to connect to an external database, IQueryable is the best option.

Frequently Asked Questions

Which is faster IEnumerable or IQueryable?

IEnumerable is slower than IQueryable because IQueryable allows for query optimizations and lazy loading, whereas IEnumerable does not support query optimizations and lazy loading. IEnumerable needs the query execution to happen entirely on the client side.

What is the difference between IEnumerable & IQueryable?

The main difference between IEnumerable and IQueryable lies in their handling of data. IEnumerable is suitable for in-memory collections and supports simple iteration, while IQueryable extends this capability for composing queries against remote data sources, offering deferred execution.

What is the difference between IEnumerable and IQueryable repository?

IEnumerable retrieves all data into memory for filtering. IQueryable provides deferred execution, enabling server-side filtering and optimization, ideal for large datasets or database queries.

What is difference between IEnumerable and List in C#?

IEnumerable is an interface for iterating over a collection, while List is a concrete implementation of a collection, offering additional features like indexing and resizing.

What is the difference between IEnumerator and IEnumerable?

IEnumerator allows iteration over a collection with methods like MoveNext and Current. IEnumerable is an interface that provides the GetEnumerator method, returning an IEnumerator for iteration.

Conclusion

We would like to shake hands virtually and pat your back for making it this far. There are going to be a lot more articles surrounding these topics. We will conclude this blog by summarising what we have learned so far! We learned about what IEnumerable and IQueryable are, how it is used, and compared IEnumerable vs IQueryable to see what are the differences and similarities between them.

Before you close this page down, check out the articles our fellow ninjas wrote for you.

Methods in C#Basics of C#WebServices in C#C# DelegatesStructures in C#C# KeywordsBasic SQLSQL IntroductionAdvance SQL, and many more!

Previous article
C# Indexers
Next article
Fibonacci Series in C#
Live masterclass