๐ŸŽ“ A1Learner
๐Ÿ”
0 of 38 lessons completed

C# LINQ Introduction

LINQ is a set of methods that filter and shape the items in a collection. A LINQ query reads a source and produces a new sequence of values.

Where filters a list

Where tests each number against a rule. Only the numbers that pass the test reach the loop.

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

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 5, 2, 8, 1, 9, 3 };
        IEnumerable<int> big = numbers.Where(n => n > 3);
        foreach (int n in big)
        {
            Console.WriteLine(n);
        }
    }
}

Select shapes each value

Select turns each number into a new value. The var keyword lets the compiler fill in the result type.

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

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 2, 4, 6 };
        var doubled = numbers.Select(n => n * 2);
        foreach (int n in doubled)
        {
            Console.WriteLine(n);
        }
    }
}

Where and Select together

A query can filter first and shape second. Where keeps the large numbers and Select multiplies each one.

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

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 5, 2, 8, 1, 9, 3 };
        var result = numbers.Where(n => n > 3).Select(n => n * 10);
        foreach (int n in result)
        {
            Console.WriteLine(n);
        }
    }
}

A missing using line

This program leaves out the LINQ using line. The compiler cannot find Where and the build fails.

using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<int> numbers = new List<int> { 1, 2, 3 };
        var big = numbers.Where(n => n > 1);
        foreach (int n in big)
        {
            Console.WriteLine(n);
        }
    }
}

Example

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

class Program
{
    static void Main(string[] args)
    {
        List<int> scores = new List<int> { 45, 82, 67, 91, 30 };
        var boosted = scores.Where(n => n >= 60).Select(n => n + 5);
        foreach (int n in boosted)
        {
            Console.WriteLine(n);
        }
    }
}

Key points

  • The using System.Linq; line brings the LINQ methods into scope.
  • Where keeps only the items that pass a test.
  • Select changes each item into a new value.
  • var lets the compiler fill in the type of a variable.
  • LINQ result types are often long and hard to write by hand.
  • A query result keeps the order of its source items.

In this example, Where keeps the scores of 60 or more. Select adds 5 to each kept score. The loop prints 87, 72 and 96 in source order.

Note

Where and Select can chain in one query.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does var do in the line var result = numbers.Where(n => n > 3);?