Основы LINQ
string[] teams = {"Desolator", "Daedalus", "Butterfly", "Silver Edge", "Power Treads", "Satanic"};
var selectedTeams = new List<string>();
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<User> users = new List<User>
{
new User {
Name="Invoker",
Age=23,
Languages = new List<string> {
"английский", "немецкий" }},
new User {
Name="Pudge",
Age=27,
Languages = new List<string> {
"английский", "французский" }},
new User {
Name="Crystal maiden",
Age=29,
Languages = new List<string> {
"английский", "испанский" }},
new User {
Name="Shadow Fiend",
Age=24,
Languages = new List<string> {
"испанский", "немецкий" }}
};
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<string> Languages { get; set; }
}
Вывод:
Pudge - 27
Crystal maiden - 29
Пятая штучка
List<User> users = new List<User>()
{
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