typestar

Query syntax in C#

The same LINQ, shaped like the SQL it borrowed from.

var scores = new[] { 91, 62, 85, 45, 78, 96 };

// query syntax: the same LINQ, shaped like SQL
var passing =
    from s in scores
    where s >= 60
    orderby s descending
    select s;

Console.WriteLine(string.Join(",", passing));
Console.WriteLine(passing.First());

How it works

  1. from ... where ... select reads top to bottom.
  2. orderby s descending sorts inside the query.
  3. The result is a normal lazy sequence; First() still works.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
254
Tokens
63
Three-star pace
95 tpm

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

Type this snippet

Step 1 of 3 in Query syntax, step 10 of 17 in LINQ in depth.

← Previous Next →