Difference between IQueryable, ICollection, IList & IDictionary interface

后端 未结 3 1140
逝去的感伤
逝去的感伤 2020-12-07 09:41

I am trying to understand difference between IQueryable, ICollection, IList & IDictionary interface which is more faster for basic operations like iterating, Indexing, Q

3条回答
  •  庸人自扰
    2020-12-07 10:27

    IQueryable,IList,IDictionary,ICollection inherits IEnumerable Interface. All the interfaces of type collection will inherits IEnumerable Interface.

    Differences Between IEnumerable and IQueryable IEnumerable:-when 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 _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 _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

    1. If you are writing the query against the data in the database then use IQueryable.
    2. If you are querying against the in process data then use IEnumerable. IEnumerable has a methods GetEnumerator(),Current() and MoveNext().

提交回复
热议问题