LINQ queries in Visual Basic
From, Where and Order By: SQL-shaped queries over any collection.
Dim rivers = {"Nile", "Amazon", "Danube", "Po", "Mekong", "Ob"}
Dim longNames = From r In rivers
Where r.Length > 2
Order By r.Length Descending, r
Select r.ToUpper()
For Each name In longNames
Console.WriteLine(name)
Next
Dim shortest = (From r In rivers Order By r.Length Select r).First()
Console.WriteLine($"shortest: {shortest}")
How it works
- The query reads like SQL inside out: source first, Select last.
Order By r.Length Descending, rsorts by two keys at once.- Queries are lazy —
First()on a parenthesized query forces a value.
Keywords and builtins used here
DimEachForNextSelect
The run, in numbers
- Lines
- 13
- Characters to type
- 337
- Tokens
- 96
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 68 seconds.
Step 1 of 3 in LINQ, step 10 of 29 in Language basics.