LINQ

Querying collections, objects and databases from C#.

LINQ is taught here in 14 lessons, running from Query syntax and method syntax through to PLINQ and parallel queries. Each lesson takes one topic, shows the working code, and links onward to the next, so the course can be read straight through in order.

Track.NET
Lessons14
LevelBeginner to intermediate
Reading timeabout 3 hours
PrerequisitesHelpful, but not required: ASP.NET Core

Lessons

  1. Query syntax and method syntaxThe two spellings of LINQ, how the compiler rewrites one into the other, and which to reach for in a given situation.
  2. Common LINQ operatorsFiltering, projection, ordering, aggregation and the element operators, with the traps that produce exceptions in production.
  3. Deferred execution and querying a databaseWhy a query does nothing until you enumerate it, the IEnumerable versus IQueryable divide, and how EF Core turns expressions into SQL.
  4. Lambda expressions and extension methods behind LINQEvery query operator is an extension method taking a delegate, and knowing the difference between a delegate and an expression tree explains IQueryable.
  5. Projection and shaping resultsSelect and SelectMany decide the shape of the data, and projecting in the query rather than after it is the cheapest performance work in LINQ.
  6. Grouping, joining and set operationsGroupBy returns a sequence of groups, joins have two syntaxes, and set operations compare with the default equality comparer unless you say otherwise.
  7. Sorting, equality and custom comparersOrderBy chains, sort stability, and the equality rules that quietly decide the result of Distinct, GroupBy and every set operator.
  8. Aggregation and statistical queriesSum, Average, Min and Max throw on an empty sequence of non-nullable values, and the aggregate operators are the ones that surprise people in production.
  9. LINQ to XML and XDocument queriesXDocument is a queryable in-memory tree with namespaces as first-class objects, which is where most LINQ to XML code goes wrong.
  10. Debugging and testing LINQ queriesBreak the pipeline, read the SQL, and write assertions that pin your behaviour without re-testing the framework.
  11. Async LINQ and streaming with IAsyncEnumerableAwait foreach, the operators EF Core provides, and why System.Linq.Async is a tool with a real cost that is not always worth paying.
  12. Query performance and avoiding N+1Multiple enumeration, N+1 queries, missing indexes and the measurement discipline that stops you from optimising the wrong thing.
  13. Expression trees and IQueryable internalsAn IQueryable is a description of a query, and the difference between a delegate and an expression tree is the whole reason providers can translate it.
  14. PLINQ and parallel queriesAsParallel splits work across cores, which only pays off for CPU-bound work over enough items with no shared state.

More in .NET

.NET ASP.NET Core WinForms WPF

FAQ

How long does the LINQ course take?
It has 14 lessons, about 3 hours of reading. Expect roughly twice that if you type out and run every example.
Do I need prior experience for LINQ?
Not strictly. It helps to have read ASP.NET Core first, because some lessons build on it, but every lesson explains its own assumptions.
What should I read after LINQ?
Continue with WinForms (14 lessons), the next course in .NET.