Skip to content

DE CSharp Keyword on

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

C#: on

Handbuch C# · Praktischer Leitfaden

Gibt die Verknüpfung mit der linken Taste LINQ vor Gleichheit an.

Einfaches Beispiel (Example.cs)

Speichern Sie dieses Beispiel separat als Example.cs. Führen Sie es unabhängig von den anderen Beispielen dieser Seite aus.

Example.cs:

// PROJECT-EXAMPLE
using System.Linq;
int[] values = [1, 2, 3];
var result = from n in values join other in new[] { 2 } on n equals other select n;
System.Console.WriteLine(string.Join(",", result));

Ergebnis:

2

Syntax

join ... on leftKey equals rightKey

Die Abfrage wird während des Durchlaufs normalerweise verzögert ausgeführt. ToArray/ToList erfasst das Ergebnis in einer neuen Sammlung.

Beispiel 1: Grundform

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        var items = new[] { new { Id = 1, Name = "ore" }, new { Id = 2, Name = "wood" } };
        var counts = new[] { new { Id = 1, Count = 4 }, new { Id = 2, Count = 2 } };
        var query = from item in items
                    join count in counts on item.Id equals count.Id
                    let total = count.Count * 2
                    where total > 3
                    orderby total descending, item.Name ascending
                    select new { item.Name, Total = total };
        foreach (var item in query) Console.WriteLine($"{item.Name}:{item.Total}");
    }
}

Beispiel 2: eine weitere praktische Form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        var owners = new[] { new { Id = 1, Name = "A" }, new { Id = 2, Name = "B" } };
        var items = new[] { new { OwnerId = 1, Name = "ore" } };
        var query = from owner in owners
                    join item in items on owner.Id equals item.OwnerId into owned
                    from item in owned.DefaultIfEmpty()
                    select $"{owner.Name}:{item?.Name ?? "empty"}";
        foreach (string text in query) Console.WriteLine(text);
    }
}

Offizielle Quelle: Microsoft Learn.

Clone this wiki locally