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.