实现 IQueryable的可实例化类型有哪些?在 .Net 4.0 中可用吗?
在 .Net 4.0 上的 C# 上下文中,是否有任何实现 IQueryable
的内置对象?
Within the context of C# on .Net 4.0, are there any built-in objects that implement IQueryable<T>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IQueryable
对象由可查询提供程序(例如 LINQ to SQL、LINQ to Entities/Entity Framework 等)生成。实际上,在基本 .NET Framework 中,您无法使用new
实例化的任何内容都实现了 IQueryable。IQueryable
是一个设计用于创建 Queryable 提供程序的接口,它允许通过构建可解析的表达式树来利用外部数据存储的 LINQ 库。从本质上讲,Queryable 需要上下文 - 有关您正在查询的内容的信息。使用new
创建任何 IQueryable 类型,无论是否可行,都不会让您走得太远。也就是说,任何
IEnumerable
都可以通过使用AsQueryable()
扩展方法转换为IQueryable
。这会在幕后创建一个表面上相似但功能上非常不同的构造,就像对普通的 IEnumerable 对象使用 LINQ 方法一样。这可能是您无需设置实际的 IQueryable 提供程序即可访问的最丰富的可查询源。这种转换对于基于 LINQ 的算法的单元测试非常有用,因为您不需要实际的数据存储,只需要可以模仿它的内存数据列表。IQueryable
objects are produced by Queryable Providers (ex. LINQ to SQL, LINQ to Entities/Entity Framework, etc). Virtually nothing you can instantiate withnew
in the basic .NET Framework implements IQueryable.IQueryable
is an interface designed to be used to create Queryable providers, which allow the LINQ library to be leveraged against an external data store by building a parse-able expression tree. By nature, Queryables require a context - information regarding what exactly you're querying. Usingnew
to create any IQueryable type, regardless of whether it's possible, doesn't get you very far.That being said, any
IEnumerable
can be converted into anIQueryable
by using theAsQueryable()
extension method. This creates a superficially-similar, but functionally very different construct behind the scenes as when using LINQ methods against a plainIEnumerable
object. This is probably the most plentiful source of queryables you have access to without setting up an actual IQueryable provider. This changeover is very useful for unit-testing LINQ-based algorithms as you don't need the actual data store, just a list of in-memory data that can imitate it.嗯,你的问题有点奇怪......但我相信,如果你查看 中的界面Reflector,它将为您提供已加载程序集中的实现者列表。
作为免责声明,自从 Reflector 成为付费游戏以来,我就没有使用过它,所以我可能是错的。
Well, your question is kinda weird... but I believe that if you look at an interface in Reflector, it will give you a list of implementers in the loaded assemblies.
As a disclaimer I have not used Reflector since it went pay-for-play so I might be wrong.
EntityCollection 是这样,EnumerableQuery。
我并不认为其中任何一个都能让你有所收获。为了提供帮助,我们需要知道您真正想要解决的问题是什么。如果您正在编写 LINQ 提供程序,则应该阅读以下内容:http://msdn。 microsoft.com/en-us/library/bb546158.aspx。
他们建议您编写自己的实现。
EntityCollection does, as does EnumerableQuery.
Not that I think either of these is going to get you anywhere. To help, we need to know what you are really trying to solve. If you are writing a LINQ provider, you should read this: http://msdn.microsoft.com/en-us/library/bb546158.aspx.
They recommend writing your own implementation.
如果您正在寻找一种实例化 IQueryable 空列表的方法,那么您可以使用以下方法:
If you are looking for a way to instantiate an empty list of
IQueryable
, then you can use this: