强制LINQ到没有延迟执行 [英] Force Linq to not delay execution

查看:227
本文介绍了强制LINQ到没有延迟执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在事实上,这是因为这个帖子同样的问题:

In fact, this is the same question as this post:

我怎样才能确保我的DAL调用的时候我LINQ查询执行,而不是在延迟时尚?

但由于他没有解释的为什么的他想,这个问题似乎已经过了一下过去了。这里是我的同类,但是,更好的解释的问题:

But since he didn't explain why he wanted it, the question seems to have been passed over a bit. Here's my similar-but-better-explained problem:

我的线程两种类型(忽略UI线程片刻)屈指可数。有一个数据收集螺纹类型和计算螺纹类型。数据收集线程是缓慢的。有相当多的数据,通过从不同的地方来筛选。计算线程是比较快的。设计模型到这一点是发送数据采集线程去寻找数据,并且是完整的,当他们通过进行计算数据了。

I have a handful of threads in two types (ignoring UI threads for a moment). There's a "data-gathering" thread type, and a "computation" thread type. The data gathering threads are slow. There's a quite a bit of data to be sifted through from a variety of places. The computation threads are comparatively fast. The design model up to this point is to send data-gathering threads off to find data, and when they're complete pass the data up for computation.

当我编码在我的LINQ数据收集我结束了吊装一些缓慢的的回到我的计算线程的。但是也有一些,直到他们计算过程中使用没有得到彻底解决,现在的数据元素 - 而这是一个问题。

When I coded my data gathering in Linq I wound up hoisting some of that slowness back into my computation threads. There are now data elements that aren't getting resolved completely until they're used during computation -- and that's a problem.

我想迫使LINQ到完成其在给定时间工作(语句的结束?方法结束了吗?,请完成了,该死的方法调用),让我知道我不为它付出以后。加入.ToList()到的LINQ到底是尴尬的1和2。感觉就像拳击的东西,是关于在另一个线程拆箱暂时无妨。

I'd like to force Linq to finish its work at a given time (end of statement? end of method? "please finish up, dammit" method call) so that I know I'm not paying for it later on. Adding ".ToList()" to the end of the Linq is 1. awkward, and 2. feels like boxing something that's about to be unboxed in another thread momentarily anyway.

推荐答案

您将不会被任何拳击 - 你会缓冲效果。

You wouldn't be boxing anything - you'd be buffering the results.

使用了ToList ()基本上去,如果你真的想要的数据的方式。除非你准备好立即使用的数据,它有被缓冲的地方的,不是吗?名单只是做一个便捷的方式。

Using ToList() is basically the way to go if you actually want the data. Unless you're ready to use the data immediately, it's got to be buffered somewhere, hasn't it? A list is just a convenient way to do that.

另一种方法是做加工,然后有以及 - 把数据作为你生产了,急切地问道。我不太明白的事情不同的线程的一面,所以它不是我清楚这是否会帮助你,但那些基本上都是提供给你的选择,因为据我可以看到。

The alternative is to do the processing then and there as well - use the data as you produce it, eagerly. I didn't quite follow the different threads side of thing, so it's not clear to me whether that would help you, but those are basically the choices available to you as far as I can see.

这实际上是的有些的明确在你的描述:

This is actually somewhat explicit in your description:

设计模型到这点
发送数据采集线程关闭以
查找数据,而当他们完成
通为运算处理的数据了。

The design model up to this point is to send data-gathering threads off to find data, and when they're complete pass the data up for computation.

调用了ToList()基本上改变你对数据当记者问到,可获取数据的查询返回什么本身,缓冲列表中。

Calling ToList() basically changes what you return from "a query which can fetch the data when asked to" to "the data itself, buffered in a list".

这篇关于强制LINQ到没有延迟执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆