C# 强制 Linq 不延迟执行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1064043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:21:48  来源:igfitidea点击:

Force Linq to not delay execution

c#linqdelayed-execution

提问by Clinton Pierce

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

事实上,这与这篇文章的问题是一样的:

How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?

如何确保我的 LINQ 查询在我的 DAL 中调用时执行,而不是以延迟的方式执行?

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

不过既然他没有解释为什么想要它,这个问题似乎就被忽略了一些。这是我的类似但更好解释的问题:

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.

我有两种类型的少数线程(暂时忽略 UI 线程)。有“数据收集”线程类型和“计算”线程类型。数据收集线程很慢。有相当多的数据需要从各个地方进行筛选。计算线程相对较快。到目前为止的设计模型是发送数据收集线程来查找数据,当它们完成时将数据传递给计算。

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 中对我的数据收集进行编码时,我最终将这种缓慢的部分提升回了我的计算线程。现在有些数据元素在计算过程中使用之前不会得到完全解析——这是一个问题。

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.

我想强制 Linq 在给定时间(语句结束?方法结束?“请完成,该死的”方法调用)完成它的工作,以便我知道我以后不会为此付费。将“.ToList()”添加到 Linq 的末尾是 1. 尴尬,以及 2. 感觉就像在另一个线程中暂时将要拆箱的东西装箱。

采纳答案by Jon Skeet

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

你不会拳击任何东西 - 你会缓冲结果。

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.

ToList()如果你真的想要数据,使用基本上是要走的路。除非您准备好立即使用数据,否则必须将其缓存在某处,不是吗?列表只是一种方便的方法。

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 somewhatexplicit 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.

到目前为止的设计模型是发送数据收集线程来查找数据,当它们完成时将数据传递给计算。

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".

调用ToList()基本上将您返回的内容从“可以在被要求时获取数据的查询”更改为“数据本身,缓冲在列表中”。

回答by JaredPar

Can you explain more why .ToList is not acceptable? You mentioned boxing and unboxing but those are completely unrelated topics.

你能解释更多为什么 .ToList 是不可接受的吗?您提到了装箱和拆箱,但这些是完全不相关的主题。

Part of forcing a LINQ query to complete on demand necessitates storing the results. Otherwise in order to see the results again, you'd have to repprocess the query. .ToList efficiently achieves this by storing the elements in a List<T>.

强制 LINQ 查询按需完成的一部分需要存储结果。否则,为了再次查看结果,您必须重新处理查询。.ToList 通过将元素存储在 .ToList 中有效地实现了这一点List<T>

It's possible to store the elements in virtually any other collection style data structure with various trade offs that may suit your needs better.

可以将元素存储在几乎任何其他集合样式的数据结构中,并进行各种权衡以更好地满足您的需求。

回答by leppie

There is a LoadOptionsproperty in the DataContextclass that could help you fetch the data more eagerly.

类中有一个LoadOptions属性DataContext可以帮助您更急切地获取数据。

Else you could use a few clever placed ToList()'s.

否则你可以使用一些巧妙的放置ToList()

回答by Brent Anderson

I know this thread is old... anyway, funny no-one mentioned .ToLast() yet. I'm doing something where linq is not much more than a glorified foreach driving some side effects where I don't really care about the query result... so I didn't want to allocate any more bogus memory than necessary.

我知道这个线程很旧......无论如何,有趣的是没有人提到 .ToLast() 呢。我正在做一些事情,其中​​ linq 只不过是一个美化的 foreach 驱动一些副作用,我并不真正关心查询结果......所以我不想分配任何不必要的虚假内存。