Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

by: practical reference

by: practical reference: by separates an element from its key in a query group ... by clause.

Version: C# language and .NET 10 reference runtime

Syntax

by /* grammar-specific form */

by separates an element from its key in a query group ... by clause.

Use it in query syntax to group by a computed key.

It is not a general keyword outside the query context.

null keys and comparer behavior depend on the target provider or operator.

The complete program actually uses by; its exact expected stdout is 0:1;1:3.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var values = new[] { 1, 2, 1, 3 };
var groups = from value in values
             group value by value % 2 into parity
             orderby parity.Key
             select $"{parity.Key}:{parity.Count()}";
Console.WriteLine(string.Join(";", groups));

Result

0:1;1:3

The complete program actually uses `by`; its exact expected stdout is `0:1;1:3`.

Sources