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

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?

3 Don't forget First(), Single(), or FirstOrDefault() or SingleOrDefault() – Randolpho Jun 18 '09 at 15:08 Very true but I think they wanted to get the entire sequence. – Andrew Hare Jun 18 '09 at 15:08 I was wondering, does . FirstOrDefault and similar methods force linq query execution?

Just wanted to confirm this... – Erx_VB.NExT. Coder Dec 21 '09 at 6:36 @Erx_VB.NExT. Coder - Yes, the methods that Randolpho mentions above in his comment do force execution.

– Andrew Hare Dec 21 '09 at 11:46.

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?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions