Force Linq to not delay execution

前端 未结 4 1997
礼貌的吻别
礼貌的吻别 2021-02-12 15:12

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?

But since he didn\'

4条回答
  •  臣服心动
    2021-02-12 16:02

    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.

    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.

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

提交回复
热议问题