Reputation: 2547
I am trying to understand difference between IQueryable, ICollection, IList & IDictionary interface which is more faster for basic operations like iterating, Indexing, Querying and more.
which class like Collection, List, Dictionary etc would be good to initiating with these interfaces and when should we be using these class. Basic advantages of using these classes over others.
I tried reading other posts with similar questions but nothing answered my full questions. Thanks for the help.
Upvotes: 60
Views: 44265
Reputation: 17652
All of these interfaces inherit from IEnumerable
, which you should make sure you understand. That interface basically lets you use the class in a foreach
statement (in C#).
ICollection
is the most basic of the interfaces you listed. It's an enumerable interface that supports a Count and that's about it.IList
is everything that ICollection
is, but it also supports adding and removing items, retrieving items by index, etc. It's the most commonly-used interface for "lists of objects", which is vague I know.IQueryable
is an enumerable interface that supports LINQ. You can always create an IQueryable
from an IList
and use LINQ to Objects, but you also find IQueryable
used for deferred execution of SQL statements in LINQ to SQL and LINQ to Entities.IDictionary
is a different animal in the sense that it is a mapping of unique keys to values. It is also enumerable in that you can enumerate the key/value pairs, but otherwise it serves a different purpose than the others you listed.The MSDN documentation for each of these is decent, so I would start there in rounding out your understanding.
Upvotes: 127
Reputation: 25887
I noticed few issues in gunny229's answer. I've mentioned those issues in comment area of his post. Later on, I thought to write a more detailed post to connect some missing dots.
Disclaimer: I don't intend to cater OP's question in entirety but I want to point out the difference between IQueryable and IEnumerable when using LINQ to SQL.
I created following structure in DB (DDL script):
CREATE TABLE [dbo].[Employee]([PersonId] [int] NOT NULL PRIMARY KEY,[Salary] [int] NOT NULL)
Here is the record insertion script (DML script):
INSERT INTO [EfTest].[dbo].[Employee] ([PersonId],[Salary])VALUES(1, 20)
INSERT INTO [EfTest].[dbo].[Employee] ([PersonId],[Salary])VALUES(2, 30)
INSERT INTO [EfTest].[dbo].[Employee] ([PersonId],[Salary])VALUES(3, 40)
INSERT INTO [EfTest].[dbo].[Employee] ([PersonId],[Salary])VALUES(4, 50)
INSERT INTO [EfTest].[dbo].[Employee] ([PersonId],[Salary])VALUES(5, 60)
GO
Now my goal was to get top 2 records from Employee table in database. I created a new C# console application in Visual Studio (VS). Then, I added an ADO.NET Entity Data Model XML (EDMX) item pointing to Employee table in the database. Now I can start writing LINQ queries.
Case I: Code for IQueryable route
using (var efContext = new EfTestEntities())
{
IQueryable<int> employees = from e in efContext.Employees select e.Salary;
employees = employees.Take(2);
foreach (var item in employees)
{
Console.WriteLine(item);
}
}
Before running this program, I had started a session of SQL Query profiler on the SQL Server instance. Here is the summary of execution:
Total number of queries fired: 1
Query text:
SELECT TOP (2) [c].[Salary] AS [Salary] FROM [dbo].[Employee] AS [c]
We can see that IQueryable is smart enough to apply the Top (2)
clause on database server side itself. So it brings only 2 out of 5 records over the network. No more in-memory filtering is required on the client side.
Case II: Code for IEnumerable route
using (var efContext = new EfTestEntities())
{
IEnumerable<int> employees = from e in efContext.Employees select e.Salary;
employees = employees.Take(2);
foreach (var item in employees)
{
Console.WriteLine(item);
}
}
Summary of execution in this case:
Total number of queries fired: 1
Query text captured in SQL profiler:
SELECT [Extent1].[Salary] AS [Salary] FROM [dbo].[Employee] AS [Extent1]
Now the thing to note is that IEnumerable brought all the 5 records present in Salary table and then performed an in-memory filteration on the client side to get top 2 records. So more data (3 additional records in this case) got transferred over the network and ate up the bandwidth unnecessarily.
Upvotes: 11
Reputation: 57
IQueryable,IList,IDictionary,ICollection inherits IEnumerable Interface. All the interfaces of type collection will inherits IEnumerable Interface.
Differences Between IEnumerable and IQueryable IEnumerable: Wen you are running a query which returns of type IEnumerable. IEnumerable will first Executes the first query and then executes the sub queries written for that query.
Example: If you want get the top 10 population Records from a Particular country then the query we will use in LINQ is
IEnumerable<LongInt> _Population=from s in India select s.population;//First Query
_Population=_Population.take(10);//Second Query
Now if we execute this query First Query will be executed first the IEnumerable will get the Records of all the population from the sql server then it will store the data in the In Process memory and then it executes the top 10 population in the next Query.(Execution is taken place for two times on sql server).
If we execute the same query by using IQueryable .
IQueryable<LongInt> _Population=from s in India select s.population;//First Query
_Population=_Population.take(10);//Second Query
in this IQueryable it will execute the two Queries at the same time in the sense it will get the population which is of top 10 in a single Query instead of getting the data and filtering the first 10 again.(One time execution on sql server).
Conclusion for IQueryable and IEnumerable
Upvotes: 3