What is the difference between IQueryable and IEnumerable?

前端 未结 12 1617
梦毁少年i
梦毁少年i 2020-11-22 06:41

What is the difference between IQueryable and IEnumerable?


See also What\'s the difference between IQueryable and I

相关标签:
12条回答
  • 2020-11-22 07:01

    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 (e.g., 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.

    0 讨论(0)
  • 2020-11-22 07:01

    We use IEnumerable and IQueryable to manipulate the data that is retrieved from database. IQueryable inherits from IEnumerable, so IQueryable does contain all the IEnumerable features. The major difference between IQueryable and IEnumerable is that IQueryable executes query with filters whereas IEnumerable executes the query first and then it filters the data based on conditions.

    Find more detailed differentiation below :

    IEnumerable

    1. IEnumerable exists in the System.Collections namespace
    2. IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data
    3. IEnumerable is suitable for querying data from in-memory collections like List, Array
    4. IEnumerable is beneficial for LINQ to Object and LINQ to XML queries

    IQueryable

    1. IQueryable exists in the System.Linq namespace
    2. IQueryable executes a 'select query' on server-side with all filters
    3. IQueryable is suitable for querying data from out-memory (like remote database, service) collections
    4. IQueryable is beneficial for LINQ to SQL queries

    So IEnumerable is generally used for dealing with in-memory collection, whereas, IQueryable is generally used to manipulate collections.

    0 讨论(0)
  • 2020-11-22 07:04

    In simple words other major difference is that IEnumerable execute select query on server side, load data in-memory on client side and then filter data while IQueryable execute select query on server side with all filters.

    0 讨论(0)
  • 2020-11-22 07:06

    Here is what I wrote on a similar post (on this topic). (And no, I don't usually quote myself, but these are very good articles.)

    "This article is helpful: IQueryable vs IEnumerable in LINQ-to-SQL.

    Quoting that article, 'As per the MSDN documentation, calls made on IQueryable operate by building up the internal expression tree instead. "These methods that extend IQueryable(Of T) do not perform any querying directly. Instead, their functionality is to build an Expression object, which is an expression tree that represents the cumulative query. "'

    Expression trees are a very important construct in C# and on the .NET platform. (They are important in general, but C# makes them very useful.) To better understand the difference, I recommend reading about the differences between expressions and statements in the official C# 5.0 specification here. For advanced theoretical concepts that branch into lambda calculus, expressions enable support for methods as first-class objects. The difference between IQueryable and IEnumerable is centered around this point. IQueryable builds expression trees whereas IEnumerable does not, at least not in general terms for those of us who don't work in the secret labs of Microsoft.

    Here is another very useful article that details the differences from a push vs. pull perspective. (By "push" vs. "pull," I am referring to direction of data flow. Reactive Programming Techniques for .NET and C#

    Here is a very good article that details the differences between statement lambdas and expression lambdas and discusses the concepts of expression tress in greater depth: Revisiting C# delegates, expression trees, and lambda statements vs. lambda expressions.."

    0 讨论(0)
  • 2020-11-22 07:07

    IQueryable is faster than IEnumerable if we are dealing with huge amounts of data from database because,IQueryable gets only required data from database where as IEnumerable gets all the data regardless of the necessity from the database

    0 讨论(0)
  • 2020-11-22 07:10

    The primary difference is that the LINQ operators for IQueryable<T> take Expression objects instead of delegates, meaning the custom query logic it receives, e.g., 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
    • 2018: "THE MOST FUNNY INTERFACE OF THE YEAR … IQUERYABLE<T>" from Bart De Smet.

    http://www.codeproject.com/KB/cs/646361/WhatHowWhere.jpg

    0 讨论(0)
提交回复
热议问题