逐步创建查询,我应该使用 IEnumerable 还是 IQueryable? [复制]
Posted
技术标签:
【中文标题】逐步创建查询,我应该使用 IEnumerable 还是 IQueryable? [复制]【英文标题】:Create a query step by step, should I use IEnumerable or IQueryable? [duplicate] 【发布时间】:2019-09-04 18:02:22 【问题描述】:我正在使用 Entity Framework 6 和 vb.net 2017。我有两种情况需要逐步创建查询:
query = (From t in context.myobj1s select t)
if (condition1) then
query = query.where(Function(t2) t2.value1 < 5)
If (condition2) then
query = query.where(Function(t2) t2.value2 > 120)
query.tolist
和
query = (From t in context.myobj1s.Local select t)
if (condition1) then
query = query.where(Function(t2) t2.value1 < 5)
If (condition2) then
query = query.where(Function(t2) t2.value2 > 120)
Mybindingsource.Datasource = query.tolist
我的问题是:在每种情况下我应该如何声明查询:
Dim query as IEnumerable(of myobj1)
或
Dim query as IQueryable(of myobj1)
【问题讨论】:
What is the difference between IQueryable<T> and IEnumerable<T>? 的可能重复项,尤其是。 this answer. 【参考方案1】:IQueryable(of myobj1) 如果您要使用与数据库相关的方法,则更可取。例如,Include(...) 方法是基于 IQueryable 实现的。
IQueryable 是从 IEnumerable 扩展而来的
public interface IQueryable<out T> : System.Collections.Generic.IEnumerable<out T>, System.Linq.IQueryable
【讨论】:
以上是关于逐步创建查询,我应该使用 IEnumerable 还是 IQueryable? [复制]的主要内容,如果未能解决你的问题,请参考以下文章