Collections and LINQ
LINQ turns filtering and shaping collections into readable pipelines.
What you will be able to do
- Use Dictionary for lookup by key; do not scan a list repeatedly.
- Prefer Any() over Count() > 0 when you only need existence.
- Materialize with ToList only when you need a concrete list.
In plain English
List and Dictionary cover most day-to-day storage. LINQ adds composition: Where, Select, OrderBy and GroupBy. A LINQ query is not executed until you enumerate it, so keep the pipeline close to the use site.
var lessons = new[]
{
new { Title = "CSS Grid", Minutes = 16 },
new { Title = "Flexbox", Minutes = 15 },
new { Title = "Semantic HTML", Minutes = 13 },
};
var shortLessons = lessons
.Where(lesson => lesson.Minutes <= 15)
.OrderBy(lesson => lesson.Minutes)
.Select(lesson => lesson.Title)
.ToList();
var total = lessons.Sum(lesson => lesson.Minutes);Your turn
MediumFilter and sort
Given a list of lessons with Minutes, keep those under 15 minutes and order them ascending.
CSHARP · starter
var result = lessons;Check your understanding before moving on.
Knowledge checkQuestion 1
Which method checks whether any item matches without counting the whole collection?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.