English
Summary
Conclusions
LINQ lets you describe queries over sequences declaratively: the operators Where, Select, OrderBy, GroupBy, Join, and others are extension methods for IEnumerable<T> that accept lambda expressions, and query syntax compiles into the same calls. Most operators use deferred execution: a query processes data during iteration, sees changes to the source, and runs again on each iteration; ToList and ToArray capture the result. Element operators react differently to an empty sequence and duplicates, aggregate operators calculate totals, GroupBy and joins build reports from several sources, and the new .NET 9–10 operators (CountBy, AggregateBy, Index, LeftJoin) shorten typical queries. Internally, LINQ is built on iterators, so your own operators are created the same way.
Self-check questions
- What is LINQ? Which interface is LINQ to Objects built on?
- How do method syntax and query syntax differ?
- What are
Where,Select, andSelectManyused for? - What is an anonymous type? What are its limitations?
- How do you sort a sequence by several keys?
- What is deferred execution? Which operators execute immediately?
- How do
First,FirstOrDefault, andSinglediffer? - How does
MaxBydiffer fromMax? - How does
GroupBywork? What isIGrouping<TKey, TElement>? - What do
CountByandAggregateByreturn? - How does
Joindiffer fromGroupJoinandLeftJoin? - How do you implement pagination with LINQ?
- How do you implement your own LINQ operator?
- Why is
Any()better thanCount() > 0for checking whether there are elements?
Useful links
- LINQ: https://learn.microsoft.com/dotnet/csharp/linq/
- LINQ queries: https://learn.microsoft.com/dotnet/csharp/linq/get-started/introduction-to-linq-queries
- Standard query operators: https://learn.microsoft.com/dotnet/csharp/linq/standard-query-operators/
- The
Enumerableclass: https://learn.microsoft.com/dotnet/api/system.linq.enumerable - What's new in .NET 10 libraries: https://learn.microsoft.com/dotnet/core/whats-new/dotnet-10/libraries