Search code examples
c#.netlinqlinq-to-sqllinq-to-entities

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?

Solution

  • 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.