What is the difference between IQueryable and IEnumerable?

前端 未结 12 1640
梦毁少年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:10

    The primary difference is that the LINQ operators for IQueryable 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 is great for working with sequences that are iterated in-memory, but
    • IQueryable 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 and IQueryable
    • C# 3.0 and LINQ.
    • "Returning IEnumerable vs IQueryable"
    • Reactive Programming for .NET and C# Developers - An Introduction To IEnumerable, IQueryable, IObservable, and IQbservable
    • 2018: "THE MOST FUNNY INTERFACE OF THE YEAR … IQUERYABLE" from Bart De Smet.

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

提交回复
热议问题