Repository navigation
EN CSharp LINQ
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
LINQ processes the sequences: Where filters, Select transforms, OrderBy sorts, GroupBy groups, Any/All checks the condition, Count/Sum aggregates. Many operations are executed deferred while the result is enumerated; ToArray and ToList capture the picture.
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 selected = values.Where(value => value > 1);
System.Console.WriteLine(string.Join(",", selected));Result:
2,3
int[] amounts = [1, 5, 2, 10];
var selected = amounts.Where(value => value >= 5).OrderBy(value => value).ToArray();
UO.Print(string.Join(", ", selected));var entries = new[]
{
new { Name = "ore", Amount = 2 },
new { Name = "wood", Amount = 3 },
new { Name = "ore", Amount = 4 }
};
var totals = from entry in entries
group entry by entry.Name into groupValue
select new { Name = groupValue.Key, Total = groupValue.Sum(value => value.Amount) };
foreach (var total in totals)
{
UO.Print($"{total.Name}: {total.Total}");
}First throws an exception if there is no element; FirstOrDefault returns the default value. Re-enumerating a deferred request may again read the changed data, so take a snapshot of the game list carefully.
Official Documentation: LINQ, Data Retrieval and Transformation - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE