In this Video, I am going to discuss IEnumerable and IQueryable in C# with examples. Please read our previous Video where we discussed the different ways to write LINQ queries with examples. In this Video, we are going to discuss the following concepts in detail.

- What is IEnumerable in C#?
- What is IQueryable in C#?
- Examples of IEnumerable and IQueryable
In our previous Video, we write the following program using LINQ Query Syntax.
In the above example, we use the var keyword to create the variable and store the result of the LINQ query. So lets check what is the type of the variable? In order to check this, just mouseover the pointer on to the QuerySynntax variable and you will see that the type is IEnumerable<int> which is a generic type. So it is important to understand what is IEnumerable?
So in the above example, instead of writing the var keyword, you can also write IEnumerable<int> and it should work as expected as shown below.
With this keep in mind, let us understand what is IEnumerable?
What is IEnumerable in C#?
Let us first see the definition of the IEnumerable which is given below.
As we see from the above image, IEnumerable is an interface that is available in System.Collection namespace. The IEnumerable interface is a type of iteration design pattern. It means we can iterate on the collection of the type IEnumerable. As you can see in the above definition, the IEnumerable interface has one method called GetEnumerator which will return an IEnumerator that iterates through a collection.
The IEnumerable interface has also a child generic interface i.e. IEnumerable<T>. Let see the definition of IEnumerable<T> interface.
The most important point that you need to remember is, in C#, all the collection classes (both generic and non-generic) implements the IEnumerable interface. Let us prove this by visiting the definition of List<T> generic collection class as shown in the below image.
Now let us see the definition of ArrayList collection which is a non-generic collection class.
Note: The IEnumerable or IEnumerable<T> interface should be used only for in-memory data objects. Why that we will discuss in our next Video.
Let us see an example to understand C# IEnumerable with complex type.
When we execute the program it will display the result as expected as shown in the below image.
What is IQueryable in C#?
Let us first see the definition of IQueryable as shown below.
As you can see in the above image, the IQueryable is an interface and it is available in System.Linq namespace. The IQuerable interface is a child of the IEnumerable interface. So we can store IQuerable in a variable of type IEnumerable. The IQuerable interface has a property called Provider which is of type IQueryProvider interface. Let us see the definition of IQueryProvider.
The methods provided by the IQueryProvider are used to create all Linq Providers. So, this is the best choice for other data providers such as Linq to SQL, Linq to Entities, etc. Why that we will discuss in our next Video.
Let us see an example to understand C# IQueryable interface.
Now run the application and you will see the data as expected. The point that you need to remember is in order to return a collection of type IQueryable, first, you need to call the AsQueryable() method on the collection as we did in the above example.