### Основы LINQ ``` string[] teams = {"Desolator", "Daedalus", "Butterfly", "Silver Edge", "Power Treads", "Satanic"}; var selectedTeams = new List(); foreach(string s in teams) { if (s.ToUpper().StartsWith("D")) selectedTeams.Add(s); } selectedTeams.Sort(); foreach (string s in selectedTeams) Console.WriteLine(s); ``` #### Вывод: ``` Daedalus Desolator Process finished with exit code 0. ``` ### Вторая штучка ``` string[] teams = {"Desolator", "Daedalus", "Butterfly", "Silver Edge", "Power Treads", "Satanic"}; var selectedTeams = from t in teams // определяем каждый объект из teams как t where t.ToUpper().StartsWith("S") //фильтрация по критерию orderby t // упорядочиваем по возрастанию select t; // выбираем объект foreach (string s in selectedTeams) Console.WriteLine(s); ``` #### Вывод: ``` Satanic Silver Edge Process finished with exit code 0. ``` ### Третья штучка ``` string[] teams = {"Desolator", "Daedalus", "Butterfly", "Silver Edge", "Power Treads", "Satanic"}; var selectedTeams = teams .Where(t=>t.ToUpper().StartsWith("B")) .OrderBy(t => t); foreach (string s in selectedTeams) Console.WriteLine(s); ``` #### Вывод: ``` Butterfly Process finished with exit code 0. ``` ### Четвертая штучка ``` using System; List users = new List { new User { Name="Invoker", Age=23, Languages = new List { "английский", "немецкий" }}, new User { Name="Pudge", Age=27, Languages = new List { "английский", "французский" }}, new User { Name="Crystal maiden", Age=29, Languages = new List { "английский", "испанский" }}, new User { Name="Shadow Fiend", Age=24, Languages = new List { "испанский", "немецкий" }} }; var selectedUsers = users.Where(u => u.Age > 25); foreach (User user in selectedUsers) Console.WriteLine($"{user.Name} - {user.Age}"); public class User { public string Name { get; set; } public int Age { get; set; } public List Languages { get; set; } } ``` #### Вывод: ``` Pudge - 27 Crystal maiden - 29 ``` ### Пятая штучка ``` List users = new List() { new User { Name = "Timbersaw", Age = 33 }, new User { Name = "Invoker", Age = 30 }, new User { Name = "Timbersaw", Age = 21 }, new User { Name = "Spectre", Age = 43 } }; var result = users .OrderBy(u => u.Name) .ThenBy(u => u.Age); foreach (User u in result) Console.WriteLine($"{u.Name} - {u.Age}"); public class User { public string Name { get; set; } public int Age { get; set; } } ``` #### Вывод: ``` Spectre - 43 Timbersaw - 21 Timbersaw - 33 ```