IEnumerable Vs IQueryable

When working with a database, sometimes the situation may become confusing. Some questions may develop such as what we should use to fetch data from SQL Server, should I go with IEnumerable or should I go with IQueryable?

So here I am trying to show the difference between these two and you can use them depending on your requirements.

IEnumerable

System.Collection namespace

No Base Interface

Supports Deferred Execution

No Lazy Loading

No Custom Query support

Suitable for LINQ to Object and LINQ to XML Queries

When querying data from a database IEnumerable executes a SELECT query on the server side. It loads the data in-memory on the client side and does a filter.

Now we will see how IEnumerable works in real life.

I have a Table Employee in my database as in the following:

Figure 1: Employee table

Now I created a new application and right-click on the solution then select Add New Item -> LINQ To SQL Classes -> Employee.dbml.

Now write your query to select records from the Employee Table as in the following: