2

我有点困惑IQueryable界面实际代表什么。

MSDN 文档IQueryable说:“提供针对特定数据源评估查询的功能。”

文档IQueryProvider说:“定义创建和执行由 IQueryable 对象描述的查询的方法。”

名称和文档摘要表明它是一个可以查询的对象/数据存储。第二个引用和ObjectQuery实体框架中的类实现的事实IQueryable表明它是一个可以执行的查询。

我误解了什么还是真的有点模糊?

4

3 回答 3

2

AnIQueryable表示查询本身。它将有一个与之关联的数据源,但它不是数据存储本身。特别是,它通常不会将所有数据都保存在内存中。

你可能会认为它有点像一个SqlCommand对象——它有查询,它知道要请求什么数据,但它不包含数据。

于 2009-12-27T19:33:07.360 回答
1

An IQueryable represents two (arguably very distinct) things. It represents a query, in the form of a reference to an expression tree describing some code, and a data source, in the form of a reference to a "provider" which knows how to execute the query.

I think the source your confusion comes from the fact that the docs can only talk about the methods IQueryable actually defines. Most of the power of IQueryable comes from the implementation of its associated extension methods defined in the Queryable class, but they are not strictly part of the IQueryable interface.

Interesting!

Update: Rereading your question, another useful insight is that a query is itself queryable. This is key to enabling compositionality in LINQ. So an IQueryable is a query, and it is queryable - so there's no contradiction in the type name.

于 2009-12-27T19:44:23.417 回答
1

AnIQueryable表示可以对其执行查询的源。它可以是 SQL 服务器上的表的表示形式,也可以是内存中的对象集合。

AnIQueryProvider表示可以创建和执行由表达式树表示的查询的对象。例如,LINQ-to-SQL 查询提供程序将采用表达式树并返回一个IQueryable允许您在枚举之前进行枚举(以获取结果)或进一步细化(通过组合查询)的值。

在 MSDN 上有一个非常好的演练关于创建自己的IQueryProvider. 我认为,如果您执行此演练,您将有更好的理解。

于 2009-12-27T19:39:10.230 回答