简体   繁体   English

使用EF6异步查询结果

[英]async query result using EF6

I have a query that results in a 300 - 500 result set. 我有一个查询,结果为300-500。 I know it's not many items but it does take a long time to compute, something like 60 to 90 sec... 我知道项目不多,但计算时间却很长,大约需要60到90秒...
When I run the query generated by EF6 on Management studio I get 2-3 new rows each second. 当我在Management Studio上运行EF6生成的查询时,我每秒获得2-3个新行。
I would like to replicate this behavior in my grid, eg: feed the grid new rows as they become available. 我想在我的网格中复制此行为,例如:当网格中有新行可用时,将它们提供给网格。

Is there a way to do this using EF6? 有没有办法使用EF6做到这一点?

Using the "async" keyword/method forces the whole operation to hang... I used to do this using BeginExecuteReader + callback and some settings on SqlConnection... 使用“ async”关键字/方法会强制整个操作挂起...我曾经使用BeginExecuteReader +回调以及SqlConnection上的一些设置来执行此操作...

There doesn't seem to be a lot of documentation about it but it appears that Entity Framework 6 added the ForEachAsync extensions method: 似乎没有很多相关文档,但是看来Entity Framework 6添加了ForEachAsync扩展方法:

http://msdn.microsoft.com/en-us/library/system.data.entity.queryableextensions.foreachasync(v=vs.113).aspx http://msdn.microsoft.com/en-us/library/system.data.entity.queryableextensions.foreachasync(v=vs.113).aspx

Code example found on this blog 此博客上找到的代码示例

using (var context = new DataContext()) {
    await context.Manufacturers.ForEachAsync(m => Console.WriteLine("{0} : {1}", m.Name, m.Country));
}

In your item callback you could just send the results back to the UI as they become available. 在项目回调中,只要结果可用就可以将其发送回UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM