LINQ interview questions
Query operators, deferred execution, IEnumerable vs IQueryable.
4 questions with answers and explanations · 2 flashcards
Key ideas
- Most operators (
Where,Select,OrderBy) are deferred: nothing runs until you enumerate. ToList(),ToArray(),Count(),First()force immediate execution.IEnumerable<T>runs delegates in memory;IQueryable<T>builds an expression tree a provider (EF Core) translates to SQL.First()throws on empty sequences;FirstOrDefault()returnsdefault.
Questions
- What does this program print?What does this code output?Medium
- Which LINQ call executes the query immediately?Multiple choiceEasy
- Which LINQ method projects each element to a sequence and flattens the results into one sequence? (method name only)Fill in the blankMedium
- What happens when you call First() and FirstOrDefault() on an empty List<string>?Multiple choiceMedium