C# LINQ

Query collections with LINQ.

✍️ 2 exercises in this lesson

LINQ (Language Integrated Query) lets you filter, sort and transform data with expressive methods:

  • Where(): filter
  • Select(): transform
  • OrderBy() / OrderByDescending()
  • GroupBy(), Count(), Sum(), Average(), First(), Any()

The same syntax works on lists, databases (Entity Framework) and XML.

Key Takeaways: C# LINQ

Mastering C# LINQ helps you write clean and reliable C# code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C# LINQ Code Example

C#
var students = new[]
{
    new { Name = "Ali", City = "Lahore", Marks = 81 },
    new { Name = "Sara", City = "Karachi", Marks = 94 },
    new { Name = "Hina", City = "Lahore", Marks = 88 },
    new { Name = "Usman", City = "Karachi", Marks = 62 },
};

var toppers = students.Where(s => s.Marks >= 80)
                      .OrderByDescending(s => s.Marks)
                      .Select(s => s.Name);
Console.WriteLine(string.Join(", ", toppers));

foreach (var g in students.GroupBy(s => s.City))
{
    Console.WriteLine($"{g.Key}: avg {g.Average(s => s.Marks)}");
}
Output
Sara, Hina, Ali
Lahore: avg 84.5
Karachi: avg 78
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C# LINQ Practice Exercises

Check what you learned in “C# LINQ”.

/ 2 solved
Fill in the blank

Filter the items.

var top = students.(s => s.Marks >= 80);
✓ Correct! Not quite. Try again.
Multiple choice

Which LINQ method transforms each item?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services