LINQ Learning Guide: Basic to Expert
1. Introduction to LINQ
Language Integrated Query (LINQ) is a set of features in .NET for querying data in a consistent way
across different types of data sources.
2. Basic LINQ Syntax
Example:
var results = from num in numbers where num > 5 select num;
3. LINQ Query vs Method Syntax
Query syntax: similar to SQL
Method syntax: uses extension methods like .Where(), .Select()
4. Filtering (Where, OfType)
var adults = [Link](p => [Link] >= 18);
5. Projection (Select, SelectMany)
var names = [Link](p => [Link]);
6. Ordering (OrderBy, ThenBy)
var sorted = [Link](p => [Link]).ThenBy(p => [Link]);
7. Grouping (GroupBy)
var grouped = [Link](p => [Link]);
8. Joining (Join, GroupJoin)
Page 1
LINQ Learning Guide: Basic to Expert
var result = [Link](customers, o => [Link], c => [Link], (o, c) => new {...});
9. Aggregations
Sum, Average, Min, Max, Count
var total = [Link](o => [Link]);
10. Set Operations
Distinct, Union, Intersect, Except
11. Element Operators
First, FirstOrDefault, Single, SingleOrDefault, Last
12. Quantifiers
Any, All, Contains
13. Anonymous Types and Collections
var result = [Link](p => new { [Link], [Link] });
14. Deferred vs Immediate Execution
LINQ queries are deferred by default; use ToList(), ToArray() for immediate execution.
15. LINQ to Entities
LINQ with Entity Framework: [Link](p => [Link] > 18)
16. Advanced Scenarios
Page 2
LINQ Learning Guide: Basic to Expert
Nested queries, dynamic LINQ, expression trees
17. Performance Tips
Use projections, avoid unnecessary ToList(), be aware of N+1 problem
18. Common Pitfalls
Modifying collections during iteration, null references, excessive joins
19. Practice Exercises
- Filter list of strings
- Project anonymous objects
- Join two lists by key
20. Resources
- Microsoft Docs
- LINQPad
- C# Programming Guide (MSDN)
Page 3