×

c# · level 2 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

keeps the items that pass your test.

The toolbox you used for Contains holds much more. LINQ is its set of methods for asking questions of a list, like “which scores passed?”. Without that using line, C# says CS1061: it cannot find the method.

Where keeps the items that pass a test. You write the test as a , a tiny function with no name: s => s >= 50 means “take s, and say whether it is at least 50”. Where runs it on every item.

Where hands back a sequence, not a list. Call to turn it into a List, or join it straight into text with string.Join.

using System;
using System.Collections.Generic;
using System.Linq;

var scores = new List<int> { 45, 82, 67, 91, 38 };
var passed = scores.Where(s => s >= 50).ToList();
Console.WriteLine(string.Join(", ", passed));
Console.WriteLine($"{passed.Count} passed");

Read => as “goes to”: each score goes to a yes or a no.