C#idiomatic C#
oncsharp/language-reference/keywords

Language

on (C# Reference)

on keyword - C# Reference

  • page csharp/language-reference/keywords/on
  • updated 01/22/2026

Use the on contextual keyword in the join clause of a query expression to specify the join condition.


ms.custom: "updateeachrelease"

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

The following example shows how to use on in a join clause.

sample · csharpdocs/csharp/language-reference/keywords/snippets/csrefKeywordsContextual.cs12

csharp
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace csrefKeywordsContextual
{
    class Test
    {
        class Student
        {
            public string LastName;
            public Student(string name)
            {
                LastName = name;
            }
        }

        class Category
        {
            public int ID;
            public string Name;

            public Category(int i, string n)
            {
                ID = i;
                Name = n;
            }
        }

        class Product
        {
            public string Name;
            public int ProdID;
            public int CategoryID;
            public Product(string n, int pid, int cid)
            {
                Name = n;
                ProdID = pid;
                CategoryID = cid;
            }
        }

        static string[] vegetables = { "carrot", "celery" };
        static void Main()
        {
            List<Student> students = new List<Student>();
            //<Snippet10>
            var query = from student in students
                        group student by student.LastName[0];
            //</Snippet10>

            //<Snippet11>
            IEnumerable<string> sortDescendingQuery =
                from vegetable in vegetables
                orderby vegetable descending
                select vegetable;
            //</Snippet11>

            //<Snippet14>
            IEnumerable<string> sortAscendingQuery =
                from vegetable in vegetables
                orderby vegetable ascending
                select vegetable;
            //</Snippet14>

            List<Category> categories = new List<Category>();
            List<Product> products = new List<Product>();

            //<Snippet12>
            var innerJoinQuery =
                from category in categories
                join prod in products on category.ID equals prod.CategoryID
                select new { ProductName = prod.Name, Category = category.Name };
            //</Snippet12>
        }

    }
}

See also