IQueryable 和 IQueryable<T> 是异步的吗?
Are IQueryable and IQueryable<T> async?
接口 IQueryable
和 IQueryable<T>
是异步的吗?
我的意思是,假设以下代码:
void Method()
{
var customers = CustomerReposity.ToQueryable(); //The execution of query starts here.
DoSomething();
foreach (var customer in customers) // The foreach await to finish
customer.DoSomething();
}
我的推测对吗?如果不是,实现异步行为的正确形式是什么?
也许……
async void Method()
{
var customers = CustomerReposity.ToQueryable().ToListAsync(); //The execution of query starts here.
DoSomething();
foreach (var customer in await customers) // The foreach await to finish
customer.DoSomething();
}
这是个好主意吗?
不,IQueryable
和 IQueryable<T>
不是异步的,也不需要。这两个接口提供了从源中提取一些数据的方法,但是由源提供异步支持。如果源将 return 内存中的所有数据,异步使用它是没有意义的。但是,如果源 "streamed" 来自网络,那么异步枚举它可能是有意义的。
接口 IQueryable
和 IQueryable<T>
是异步的吗?
我的意思是,假设以下代码:
void Method()
{
var customers = CustomerReposity.ToQueryable(); //The execution of query starts here.
DoSomething();
foreach (var customer in customers) // The foreach await to finish
customer.DoSomething();
}
我的推测对吗?如果不是,实现异步行为的正确形式是什么?
也许……
async void Method()
{
var customers = CustomerReposity.ToQueryable().ToListAsync(); //The execution of query starts here.
DoSomething();
foreach (var customer in await customers) // The foreach await to finish
customer.DoSomething();
}
这是个好主意吗?
不,IQueryable
和 IQueryable<T>
不是异步的,也不需要。这两个接口提供了从源中提取一些数据的方法,但是由源提供异步支持。如果源将 return 内存中的所有数据,异步使用它是没有意义的。但是,如果源 "streamed" 来自网络,那么异步枚举它可能是有意义的。