What is the difference between IQueryable and IEnumerable?

前端 未结 12 1616
梦毁少年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: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.."

提交回复
热议问题