Verified Jobs Live
.NET Medium Level ~3 min read

A LINQ query runs after its database context is disposed. Why can this fail, and how would you fix it?

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

Many LINQ operations build a deferred query rather than execute it immediately. Enumerating an IQueryable later can require the database context and connection, which are no longer available after disposal. Materialize the required projection within the valid scope using an appropriate asynchronous operation, or keep execution within a properly owned unit of work. Avoid retu ing a queryable that silently exposes storage behavior across layers. Materializing everything too early can waste memory, so apply necessary filters and limits before execution and understand which operations run in the database.