如何确保在DAL中以非延迟方式调用时执行LINQ查询? [英] How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?

查看:60
本文介绍了如何确保在DAL中以非延迟方式调用时执行LINQ查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DAL,它由一堆在数据库上执行LINQ查询的方法组成.

I have a DAL that is composed of a bunch of methods that perform LINQ queries on my database.

如何确保在返回说一个IEnumberable或类似的东西之前,我确保LINQ查询先执行 then ,而不是以某种延迟的方式仅在结果为用过吗?

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?

我知道我可以在方法中对结果调用.ToList()来强制执行,但这是最好的方法吗?

I know I can call .ToList() on my result in my methods to force it to execute, but is this the best way?

推荐答案

调用 c0> ToArray 确实是最好的方法强制其执行并获取整个序列(有关强制对序列的单个元素执行的其他方法,请参阅下面的Randolpho注释).

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

您是否有特定的原因想要避免推迟执行?

Is there a specific reason you would want to avoid deferred excution?

这篇关于如何确保在DAL中以非延迟方式调用时执行LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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