IEnumerable and IQueryable in LINQ Example C#

Learn the difference between IEnumerable and IQueryable in LINQ.

What is the difference between IEnumerable and IQueryable in LINQ?

The main different is that IEnumerable only can work with object where IQueryable allows working with database object directly (like LINQ-to-SQL), so if we need to work with data from database then we should use IQueryable

IEnumerable in LINQ IQueryable in LINQ
IEnumerable comes in System.Collections Namespace IQueryable comes in System.Linq Namespace
IEnumerable is good to query data from in-memory collections like Array, List etc. IQueryable is good to work with data from out-memory (like webservice, remote database) collections.

IEnumerable and IQueryable Examples in C#

How to use IEnumerable interface? and Why?

As we have already mentioned that IEnumerable is used for querying on object, many times we come across when we want to query on our custom object, in such situation we can implement IEnumerable interface, which has a method called GetEnumerator() that again return an interface type IEnumerator.

Here is a simple example of querying on IEnumerable object

using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Book
{
    public string Subject { get; set; }
    public string BookName { get; set; }
}
We can make this call from database also, here just to avoid additional code and to show some sample data we have hardcoded some values in list
public class Student
{
public IEnumerable<book> BookList()
    {
    OldBooks = new List<book>();
    OldBooks.Add(new Book() { Subject = "Bio Tech" });
    OldBooks.Add(new Book() { Subject = "VR" });
    OldBooks.Add(new Book() { Subject = "AI" });
    OldBooks.Add(new Book() { Subject = "Data Science" });
    return OldBooks;
    }
}

In above code we have two different object Book and Student, in student object we have a BookList.

Now see the code below, how we can query on IEnumerable booklist and get the book we want, we also can loop through the collection

public class MainProgram
{
void Test()
{
Student obj = new Student();
IEnumerable<book> _books = obj.BookList();
_books.Where(b => b.BookName == "AI").FirstOrDefault<book>();       
            
        foreach (Book bo in _books)
        {
            string _bookName = bo.BookName;
        }
    }
}

When we query on any IEnumerable object means we are querying on in-memory object (not from any external sources like database, web services etc)

Actually IQueryable interface inherits from IEnumerable. Honestly there is no advantage to using IQueryable over IEnumerable

IQueryable has to be two ways implementation, The IQueryable interface is designed for implementation by query providers, a provider that also implements IQueryable<T>.

We can convert IEnumerable to IQueryable easily by using AsQueryable() method, here is an example

Student obj = new Student();
            
IQueryable<Book> _books1 = obj.BookList().AsQueryable();

So it's better to use IEnumerable

This is how you can implement IEnumerable on custom object.

using System;
using System.Collections;
public class SchoolStudent: IEnumerable
{
    public int StuId { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public List<Book> OldBooks { get; set; }
    public IEnumerator GetEnumerator()
    {
         return OldBooks.GetEnumerator();
    }
}
 
Linq IEnumerable vs IQueryable: how to use IEnumerable in linq query
LINQ (language integrated query) allow you to write query on database objects like ado.net, entity framework etc, LINQ is type safe, easy to convert database object to list objects and business objects in secure and scalable way.
linq Interview Questions Answers
LINQ C# examples | Join Asp.net MVC Course