IQueryable和DbQuery有什么区别? [英] Whats the difference between IQueryable and DbQuery?

查看:134
本文介绍了IQueryable和DbQuery有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注此问题/答案

如何使实体框架数据上下文只读

解决方案是使您的DbContext集合的类型为DbQuery,但这是一个相当专业的类型(它是

The solution is to make your DbContext collections of the type DbQuery, but that is a rather specialized type (it's buried down in the namespaces for EF).

因此,在DbContext中使用以下功能有什么区别:

So, what's the functional difference between have a DbContext with this:

public DbQuery<Customer> Customers
{
    get { return Set<Customer>().AsNoTracking(); }
}

对比:

public IQueryable<Customer> Customers
{
    get { return Set<Customer>().AsNoTracking(); }
}

...关于DbQuery,EF文档非常轻巧类,但我更喜欢让DbContext由接口而不是类组成的想法,因此我想避免这种情况。 DbQuery类还提供什么其他好处?

...the EF documentation is very light when it comes to the DbQuery class, but I prefer the idea of having DbContext made up of interfaces rather than classes so I'd like to avoid it. What additional benefits does the DbQuery class provide?

更新

答案,只是看一下代码,我意识到我的问题有点傻。我想得太快了!显然,底层的具体对象无论如何都将是DbQuery,因此实际的内部功能将是相同的。在我看来,使用IQueryable是更好的选择。谢谢您的耐心等候!

After reading the answers and just looking at the code I realized my question was a little silly. I was too quick to ask before I thought! Obviously the underlying concrete object will be a DbQuery regardless, so the actually inner functionality will be the same. It seems to me that using IQueryable is the better choice. Thanks for your patience!

推荐答案

DBQuery 是针对DbContext的非通用LINQ to Entities查询。公开此内容将为您提供针对实体的LINQ功能。如果不需要此功能,请使用 IQueryable 接口抽象。

DBQuery is a non-generic LINQ to Entities query against a DbContext. Exposing this will give you LINQ functionality against Entities. If you don't need this, use the IQueryable interface abstraction.

IOrderedQueryable

旨在由查询提供程序实现。
此接口表示排序查询的结果,该查询调用方法OrderBy,OrderByDescending,ThenBy或ThenByDescending。当调用CreateQuery并传递表示排序查询的表达式树时,所得的IQueryable对象必须为实现IOrderedQueryable的类型。

Intended for implementation by query providers. This interface represents the result of a sorting query that calls the method(s) OrderBy, OrderByDescending, ThenBy or ThenByDescending. When CreateQuery is called and passed an expression tree that represents a sorting query, the resulting IQueryable object must be of a type that implements IOrderedQueryable.

IListSource 

为对象提供功能以返回可以绑定到数据源的列表。

Provides functionality to an object to return a list that can be bound to a data source.

IDbAsyncEnumerable



IEnumerable接口的异步版本,该版本允许异步检索元素。此接口用于与Entity Framework查询进行交互,不应由自定义类实现。

Asynchronous version of the IEnumerable interface that allows elements to be retrieved asynchronously. This interface is used to interact with Entity Framework queries and shouldn't be implemented by custom classes.

这篇关于IQueryable和DbQuery有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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