IQueryable 迭代:我需要 ToListAsync() 吗?
IQueryable iteration: do I need ToListAsync()?
我有这个代码
foreach (var e in await database.Entities.Where(...).ToListAsync())
据我了解,此代码有将查询结果复制到新创建的列表中的不必要的开销,我不需要这样做。我只需要对结果进行一次迭代,如果它会保留在 EF 内部结构中,我就很好了。
另一方面,我可以使用这个
foreach (var e in database.Entities.Where(...))
但是此代码将 运行 同步,这也会对性能产生一些意想不到的影响。
在没有冗余副本的情况下异步获取结果的最佳方法是什么?
this code have an unneeded overhead of copying the query results into the newly created list, which I do not need
是的,但它是引用类型的列表。它不会复制任何实体对象本身或类似的东西,只是一个引用列表,速度非常快并且不会占用太多内存。
What is the best way to get the results asynchronously, with no redundant copy?
Asynchronous streams。但它们还不适用于 EF。所以今天 最好的方法是 ToListAsync
.
我有这个代码
foreach (var e in await database.Entities.Where(...).ToListAsync())
据我了解,此代码有将查询结果复制到新创建的列表中的不必要的开销,我不需要这样做。我只需要对结果进行一次迭代,如果它会保留在 EF 内部结构中,我就很好了。
另一方面,我可以使用这个
foreach (var e in database.Entities.Where(...))
但是此代码将 运行 同步,这也会对性能产生一些意想不到的影响。
在没有冗余副本的情况下异步获取结果的最佳方法是什么?
this code have an unneeded overhead of copying the query results into the newly created list, which I do not need
是的,但它是引用类型的列表。它不会复制任何实体对象本身或类似的东西,只是一个引用列表,速度非常快并且不会占用太多内存。
What is the best way to get the results asynchronously, with no redundant copy?
Asynchronous streams。但它们还不适用于 EF。所以今天 最好的方法是 ToListAsync
.