1

I have LINQ statement that causes a timeout when I called the FirstOrDefault(); method. But if I cast the results to a List first, the call the FirstOrDefault(); method, it works just fine.

Why would this LINQ statement cause an SQL timeout:

var n = next.Where(w => w.Status == 1).FirstOrDefault();

But this one work just fine?

var n = next.Where(w => w.Status == 1).ToList().FirstOrDefault();
bumble_bee_tuna
  • 3,533
  • 7
  • 43
  • 83
Arm0geddon
  • 466
  • 11
  • 30
  • The main difference is that the first one will end up using a `Top 1` in the query. Check out this [question](http://stackoverflow.com/questions/9616965/why-select-top-clause-could-lead-to-long-time-cost) that's about `TOP` causing timeouts. – juharr Jul 06 '15 at 16:36
  • The duplicate question should address your concern. If it does not, please improve your question by providing more details, including specific information about your database, [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem, and an explanation of why it is that the answer explaining timeouts with `TOP` doesn't apply here. – Peter Duniho Jul 06 '15 at 17:34

0 Answers0