LINQ (Language Integrated Query) filters, projects, and aggregates collections—the same mental model as EF Core queries against databases. Playground code uses System.Linq; EF translates similar expressions to SQL.
Common operators
Where— filter (.Where(u => u.Active))Select— project/shape (.Select(u => u.Email))OrderBy/ThenBy— sortingFirstOrDefault— single item or defaultAny/Count— existence and counts
Deferred vs immediate
LINQ to Objects runs in memory when enumerated. EF Core LINQ builds SQL and executes at the database—avoid pulling entire tables into memory with careless ToList() before filtering.
Important interview questions and answers
- Q: LINQ to Objects vs EF Core?
A: Same syntax; EF translates to SQL server-side, LINQ to Objects runs in CLR memory. - Q: First vs Single?
A:Firsttakes first match;Singlethrows if zero or more than one—use when uniqueness is required.
Self-check
- Write a LINQ chain that filters then projects names.
- Why filter before ToList() in EF queries?