EN CSharp Keyword ascending - LIHACHTETAN/ClassicUO-Bad-Newbie-CSharp-Scripts-IDE GitHub Wiki
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
C#: ascending
Specifies the ascending direction of the orderby key. This is the default direction, but can be written explicitly.
Simple example (Example.cs)
Save this example separately as Example.cs. Run it independently of the other examples on this page.
Example.cs:
// PROJECT-EXAMPLE
using System.Linq;
int[] values = [1, 2, 3];
var result = from n in values orderby n ascending select n;
System.Console.WriteLine(string.Join(",", result));
Result:
1,2,3
Syntax
orderby key ascending
The query is usually executed in a delayed manner during the traversal. ToArray/ToList captures the result in a new collection.
Example 1: simple 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 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}");
}
}
Example 2: another practical 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)
{
int[] values = [1, 2, 3, 4, 5];
var query = from value in values
let parity = value % 2
group value by parity into grouped
where grouped.Any()
orderby grouped.Key ascending
select new { Key = grouped.Key, Total = grouped.Sum() };
foreach (var item in query) Console.WriteLine($"{item.Key}:{item.Total}");
}
}
Official source: Microsoft Learn.