C# — LINQ
Basic LINQ
using System.Linq;
var nums = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evens = nums.Where(n => n % 2 == 0);
var doubled = nums.Select(n => n * 2);
var sum = nums.Sum();
var avg = nums.Average();
Query syntax
var result = from n in nums
where n > 5
orderby n descending
select n;
Chaining
var result = nums
.Where(n => n % 2 == 0)
.Select(n => n * n)
.Take(3)
.ToList();
GroupBy
var groups = nums.GroupBy(n => n % 2 == 0 ? "Even" : "Odd");
foreach (var group in groups)
{
Console.WriteLine($"{group.Key}: {string.Join(", ", group)}");
}
Mini Practice
Write C# code that:
- Uses Where to filter
- Uses Select to transform
- Chains multiple operations
- Uses GroupBy
Up Next
In the next lesson, you'll learn about Async Await — asynchronous programming.
Related Topics
Frequently Asked Questions about LINQ
What is LINQ in C#?
LINQ is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn LINQ?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn LINQ.
Why is LINQ important in C#?
LINQ is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.