computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

🔎

🎓

⏱ 30 min

Free

Lists & LINQ

Store collections in List and query them with LINQ — filter, sort and summarize in one line.

🎯 What you will learn

  • Use List and Dictionary
  • Filter and sort with LINQ
  • Summarize with Count, Sum, Max and Average

👋 Let’s get started!

LINQ lets you ask questions about your data in plain-ish English 🔎: “Where score > 80, order by name”. It’s one of the favorite features of C# developers.

🪜 Step by step

1. 📋 List

A list that grows.

var scores = new List<int> { 88, 95, 72, 60 };
scores.Add(100);
Console.WriteLine(scores.Count);
2. 🔎 Where & OrderBy

Filter and sort.

var high = scores.Where(s => s >= 80).OrderByDescending(s => s);
Console.WriteLine(string.Join(", ", high));
3. 🧮 Summaries

Sum, Max, Average answer big questions.

Console.WriteLine($"Best: {scores.Max()}, Avg: {scores.Average():F1}");

🧪 Try it!

Read the code, guess what it prints, then press 👀 Show output.

Loot sorter

var loot = new List<(string Name, int Value)>
{
    ("Sword", 50), ("Potion", 10), ("Gem", 120), ("Shield", 45)
};

var valuable = loot.Where(i => i.Value >= 45)
                   .OrderByDescending(i => i.Value)
                   .Select(i => i.Name);

Console.WriteLine("Keep: " + string.Join(", ", valuable));
Console.WriteLine($"Total value: {loot.Sum(i => i.Value)}");

Count passing scores

var scores = new List<int> { 55, 80, 91 };
int passed = scores.Count(s => s >= 60);
Console.WriteLine(passed);

📖 Key words

Tap a card to flip it over!

List<T>
A growable list of type T 📋
LINQ
Query data with methods like Where 🔎
Lambda =>
A tiny inline function λ
Dictionary
Key–value storage 🗝️

🏆 Challenge

Predict the output.

Challenge

var names = new List<string> { "zoe", "amir", "lia" };
var result = names.OrderBy(n => n).Select(n => n.ToUpper());
Console.WriteLine(string.Join(" ", result));

Knowledge Check Quiz

  1. What does Where do?
  2. What prints?
  3. Which sorts biggest first?
← Previous lesson
Next lesson →