What is the difference between IQueryable<T> and IEnumerable<T>?

What is the difference between IQueryable<T> and IEnumerable<T> ?


See also What's the difference between IQueryable and IEnumerable that overlaps with this question.


First of all, IQueryable<T> extends the IEnumerable<T> interface, so anything you can do with a "plain" IEnumerable<T> , you can also do with an IQueryable<T> .

IEnumerable<T> just has a GetEnumerator() method that returns an Enumerator<T> for which you can call its MoveNext() method to iterate through a sequence of T.

What IQueryable<T> has that IEnumerable<T> doesn't are two properties in particular—one that points to a query provider (eg, a LINQ to SQL provider) and another one pointing to a query expression representing the IQueryable<T> object as a runtime-traversable abstract syntax tree that can be understood by the given query provider (for the most part, you can't give a LINQ to SQL expression to a LINQ to Entities provider without an exception being thrown).

The expression can simply be a constant expression of the object itself or a more complex tree of a composed set of query operators and operands. The query provider's IQueryProvider.Execute() or IQueryProvider.CreateQuery() methods are called with an Expression passed to it, and then either a query result or another IQueryable is returned, respectively.


The primary difference is that the LINQ operators for IQueryable<T> take Expression objects instead of delegates, meaning the custom query logic it receives, eg, a predicate or value selector, is in the form of an expression tree instead of a delegate to a method.

  • IEnumerable<T> is great for working with sequences that are iterated in-memory, but
  • IQueryable<T> allows for out-of memory things like a remote data source, such as a database or web service.
  • Query execution:

  • Where the execution of a query is going to be performed "in process" , typically all that's required is the code (as code) to execute each part of the query.

  • Where the execution will be performed out-of-process , the logic of the query has to be represented in data such that the LINQ provider can convert it into the appropriate form for the out-of-memory execution - whether that's an LDAP query, SQL or whatever.

  • More in:

  • LINQ : IEnumerable<T> and IQueryable<T>
  • C# 3.0 and LINQ.
  • "Returning IEnumerable<T> vs IQueryable<T> "
  • Reactive Programming for .NET and C# Developers - An Introduction To IEnumerable , IQueryable , IObservable , and IQbservable
  • http://www.codeproject.com/KB/cs/646361/WhatHowWhere.jpg


    This is a nice video on my Facebook page which demonstrates how these interfaces differ , worth a watch.

    Below goes a long descriptive answer for it.

    The first important point to remember is IQueryable interface inherits from IEnumerable , so whatever IEnumerable can do, IQueryable can also do.

    There are many differences but let us discuss about the one big difference which makes the biggest difference. IEnumerable interface is useful when your collection is loaded using LINQ or Entity framework and you want to apply filter on the collection.

    Consider the below simple code which uses IEnumerable with entity framework. It's using a Where filter to get records whose EmpId is 2 .

    EmpEntities ent = new EmpEntities();
    IEnumerable<Employee> emp = ent.Employees; 
    IEnumerable<Employee> temp = emp.Where(x => x.Empid == 2).ToList<Employee>();
    

    This where filter is executed on the client side where the IEnumerable code is. In other words all the data is fetched from the database and then at the client its scans and gets the record with EmpId is 2 .

    But now see the below code we have changed IEnumerable to IQueryable . It creates a SQL Query at the server side and only necessary data is sent to the client side.

    EmpEntities ent = new EmpEntities();
    IQueryable<Employee> emp = ent.Employees;
    IQueryable<Employee> temp =  emp.Where(x => x.Empid == 2).ToList<Employee>();
    

    So the difference between IQueryable and IEnumerable is about where the filter logic is executed. One executes on the client side and the other executes on the database.

    So if you working with only in-memory data collection IEnumerable is a good choice but if you want to query data collection which is connected with database `IQueryable is a better choice as it reduces network traffic and uses the power of SQL language.

    链接地址: http://www.djcxy.com/p/21248.html

    上一篇: IQueryable和IEnumerable之间有什么区别

    下一篇: IQueryable <T>和IEnumerable <T>有什么区别?