实现 IQueryable<T> 的可实例化类型有哪些?在 .Net 4.0 中可用吗? [英] What instantiate-able types implementing IQueryable<T> are available in .Net 4.0?

查看:21
本文介绍了实现 IQueryable<T> 的可实例化类型有哪些?在 .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>?

推荐答案

IQueryable 对象由可查询提供程序(例如 LINQ to SQL、LINQ to Entities/Entity Framework 等)生成.在基本的 .NET Framework 中,几乎没有什么可以使用 new 实例化的实现 IQueryable.

IQueryable objects are produced by Queryable Providers (ex. LINQ to SQL, LINQ to Entities/Entity Framework, etc). Virtually nothing you can instantiate with new in the basic .NET Framework implements IQueryable.

IQueryable 是一个设计用于创建可查询提供程序的接口,它允许通过构建可解析的表达式树来针对外部数据存储利用 LINQ 库.本质上,Queryables 需要一个上下文——关于你正在查询什么的信息.使用 new 创建任何 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. Using new to create any IQueryable type, regardless of whether it's possible, doesn't get you very far.

话虽如此,任何 IEnumerable 都可以通过使用 AsQueryable() 扩展方法转换为 IQueryable.这在幕后创建了一个表面上相似但在功能上非常不同的构造,就像对普通 IEnumerable 对象使用 LINQ 方法时一样.这可能是您无需设置实际 IQueryable 提供程序即可访问的最丰富的可查询源.这种转换对于基于 LINQ 的算法进行单元测试非常有用,因为您不需要实际的数据存储,只需一个可以模仿它的内存数据列表.

That being said, any IEnumerable can be converted into an IQueryable by using the AsQueryable() extension method. This creates a superficially-similar, but functionally very different construct behind the scenes as when using LINQ methods against a plain IEnumerable 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.

这篇关于实现 IQueryable&lt;T&gt; 的可实例化类型有哪些?在 .Net 4.0 中可用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆