C# LINQ Reference Sheet (wp)

Summary

A quick cheat-sheet to help write LINQ code.

LINQ Basics

  1. LINQ is an acronym for Language Integrated Query
  2. Consists of three basic operations: Get the source data, Create the query expression and execute the query.
  3. LINQ works on collections only and not on the database like SQL.
  4. Requires the assembly using System.Linq
  5. Returns the type IEnumerable making it possible to use the foreach loop.

LINQ Standard Query Operators

Sorting Data (C#)

Set Operations (C#)

Filtering Data (C#)

Quantifier Operations (C#)

Projection Operations (C#)

Partitioning Data (C#)

Join Operations (C#)

Grouping Data (C#)

Generation Operations (C#)

Equality Operations (C#)

Element Operations (C#)

Converting Data Types (C#)

Concatenation Operations (C#)

Aggregation Operations (C#)

LINQ Anatomy

Define the source

var query = customer

Define the query expression

var query = customer

Execute the query

var query = customer

Full code implementation

 public static void ExampleThree()
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 2, 5 ,7 };

            var getNumbers = from number in numbers
                           where number  < 5
                           select number;

            foreach (var result in getNumbers)
            {
                Console.WriteLine(result);
            }
        }

RESULT:

1
2
3
4
1
2
3
4
2
3
4
2

EXPLANATION:

var – the var keyword is used to return a datatype that is unknown. Var is an anonymous type and is often used when the return type is unknown or the return type is also anonymous.

getNumbers – this is the name of the query.

from – this is a keyword which tells the compiler to retrieve data from a source.

number – this is the iterating variable. This is not a keyword therefore, this can be called anything. The iterating keyword is often shortened to a single character like ‘n‘.

in numbers – this tells the compiler to return data from our collection numbers.

where number < 5 – tells the compiler to return all numbers that are less than 5.

select number – this tells the compiler to select everything that has been returned in the query getNumbers.

Examples

Single where condition (integer)

// Simple where clause
        public static void ExampleThree()
        {
            // Source data
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 2, 5, 7 };

            // Query expression
            var getNumbers = from number in numbers
                             where number < 5
                             select number;

            // Query execution
            foreach (var result in getNumbers)
            {
                Console.WriteLine(result);
            }
        }
1
2
3
4
1
2
3
4
2
3
4
2

Single where condition (string)

 // Advanced conditions - contains characters
        public static void ExampleFour()
        {
            // Source data
            string[] names = { "Adam", "Adele", "Tesla", "Francesca", "Mark", "Sofina" };

            // Query expression
            var getNames = from n in names
                           where n.Contains("Ad")
                           select n;

            // Query execution
            foreach (var result in getNames)
            {
                Console.WriteLine(result);
            }
        }
Adam
Adele

Multiple where conditions

// Advanced conditions - contains characters
        public static void ExampleFour()
        {
            // Source data
            string[] names = { "Adam", "Adele", "Tesla", "Francesca", "Mark", "Sofina" };

            // Query expression
            var getNames = from n in names
                           where n.Contains("A") || n.Contains("T") && n.Contains("d")

                           select n;

            // Query execution
            foreach (var result in getNames)
            {
                Console.WriteLine(result);
            }
Adam
Adele

The above can also be written as:

// Advanced conditions - contains characters
        public static void ExampleFour()
        {
            // Source data
            string[] names = { "Adam", "Adele", "Tesla", "Francesca", "Mark", "Sofina" };

            // Query expression
            var getNames = from n in names
                            where n.Contains("A") 
                            where n.Contains("T") 
                            where n.Contains("d")

                           select n;

            // Query execution
            foreach (var result in getNames)
            {
                Console.WriteLine(result);
            }

Orderby

// Sort data
        // Order the values ascending/descending
        public static void ExampleFive()
        {
            // Source data
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 2, 5, 7 };

            // Query expression
            var getNumbers = from number in numbers
                             where number < 5
                             orderby number ascending       
                            // orderby number descending /* this can also be ordered by descending

                             select number;

            // Query execution
            foreach (var result in getNumbers)
            {
                Console.WriteLine(result);
            }
        }
1
1
2
2
2
2
3
3
3
4
4
4