Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does First() in LINQ cause eager or lazy loading?

I have a query:

db.Order.Include("OrderItem").First(r => r.Id == OrderId)
  1. Does First() on it own cause Eager or Lazy loading?
  2. If not then how would you force Eager loading for a First()?
  3. Is the above combined query Eagerly or lazily loaded?
like image 488
SamJolly Avatar asked Dec 13 '25 01:12

SamJolly


1 Answers

First() and FirstOrDefault() are executed immediately (eager) at the point where they're called.

All standard LINQ operators, which return a single, non-enumerable result, and those that do not return an explicit IEnumerable<T>, are executed immediately.

See Classification of Standard Query Operators by Manner of Execution for the complete list of LINQ operators.

like image 106
Nic Avatar answered Dec 14 '25 16:12

Nic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!