What is the default behavior of FirstOrDefault () when used with Linq to SQL?
He readily calculates the result of the query. The easiest way to find out about this is to understand that the return type is int , not IEnumerable<int> , which can be deferred until GetEnumerator is called, but int has no such mechanism.
The wording of your question assumes that you are also asking if there is a way to change this behavior. LINQ exists, but not directly, through FirstOrDefault or any mechanisms. But you can delay using Lazy<T> . There is no compiler, so goodbye if this does not compile, but it should be very close.
Lazy<int> value = new Lazy<int>( () => { var query = from p in context.tableX select p.Id; var result = query.FirstOrDefault(); return result; } ); if(value.Value > 0) {
jason source share