I have a DAL that is composed of a bunch of methods that perform LINQ queries on my database.
我有一个DAL,它由一堆在我的数据库上执行LINQ查询的方法组成。
How do I ensure that before returning say an IEnumberable or something similar from the database, I ensure that the LINQ query is execute then, not in some delayed fashion only to be executed when the result is used?
如何确保在返回之前从数据库中说出IEnumberable或类似的东西,我确保LINQ查询是执行的,而不是以某种延迟的方式只在执行结果时执行?
I know I can call .ToList() on my result in my methods to force it to execute, but is this the best way?
我知道我可以在我的方法中调用.ToList()来强制它执行,但这是最好的方法吗?
1 个解决方案
#1
Calling ToList
or ToArray
really is the best way to force it to execute and get the entire sequence (see Randolpho's comment below for other methods that will force execution for single elements of the sequence).
调用ToList或ToArray确实是强制它执行并获取整个序列的最佳方法(请参阅下面的Randolpho对其他方法的评论,这些方法将强制执行序列的单个元素)。
Is there a specific reason you would want to avoid deferred excution?
是否有特定原因要避免延期执行?
#1
Calling ToList
or ToArray
really is the best way to force it to execute and get the entire sequence (see Randolpho's comment below for other methods that will force execution for single elements of the sequence).
调用ToList或ToArray确实是强制它执行并获取整个序列的最佳方法(请参阅下面的Randolpho对其他方法的评论,这些方法将强制执行序列的单个元素)。
Is there a specific reason you would want to avoid deferred excution?
是否有特定原因要避免延期执行?