typestar

LINQ method syntax in Visual Basic

The same pipeline as lambdas: Where, Select, Count, Average.

Dim readings = {12.5, 48.0, 7.2, 33.1, 60.4, 21.9}

Dim alerts = readings.
        Where(Function(r) r > 30).
        Select(Function(r) $"high: {r:F1}").
        ToList()

For Each alert In alerts
    Console.WriteLine(alert)
Next

Console.WriteLine($"cool ones: {readings.Count(Function(r) r < 20)}")
Console.WriteLine($"mean: {readings.Average():F2}")

How it works

  1. Function(r) r > 30 is the filter, written inline.
  2. A trailing dot continues the chain on the next line.
  3. Count takes its own predicate; Average needs no lambda at all.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
326
Tokens
77
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 62 seconds.

Type this snippet

Step 2 of 3 in LINQ, step 11 of 29 in Language basics.

← Previous Next →