gpt4 book ai didi

c# - IQueryable 迭代 : do I need ToListAsync()?

转载 作者:太空狗 更新时间:2023-10-30 01:28:48 28 4
gpt4 key购买 nike

我有这个代码

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

关于c# - IQueryable 迭代 : do I need ToListAsync()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57007207/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com