Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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?

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

like image 215
KingNestor Avatar asked Jun 18 '09 15:06

KingNestor


1 Answers

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?

like image 70
Andrew Hare Avatar answered Nov 02 '22 13:11

Andrew Hare