kboykov 66030a4484 one | 3 months ago | |
---|---|---|
readme.md | 3 months ago |
1
// исходный массив
string[] animals = {"Кошка", "Собака", "Попугай", "Жираф", "Баран"};
// список для результатов
var selectedAnimals = new List<string>();
foreach(string s in animals)
{
// ищем слова начинающиеся на букву "С"
if (s.ToUpper().StartsWith("С"))
selectedAnimals.Add(s);
}
// сортируем получившийся список
selectedAnimals.Sort();
foreach (string s in selectedAnimals)
Console.WriteLine(s);
Ответ
Собака
2
string[] place = {"Кошка" , "Собака" , "Белка"};
var selectedAnimals = from t in place
where t.ToUpper().StartsWith("Б") //фильтрация по критерию
orderby t // упорядочиваем по возрастанию
select t; // выбираем объект
foreach (string s in selectedAnimals)
Console.WriteLine(s);
Ответ
Белка
string[] animals = { "Белка", "Кошка" , "Собака" };
var selectedAnimals = animals
.Where(t=>t.ToUpper().StartsWith("С"))
.OrderBy(t => t);
foreach (string s in selectedAnimals)
Console.WriteLine(s);
Ответ
Собака
using System;
List<User> users = new List<User>
{
new User {
Name="Милана",
Age=11,
},
new User {
Name="Катя",
Age=12,
},
new User {
Name="Лера",
Age=13,
},
new User {
Name="Мила",
Age=14,
}
};
var selectedUsers = users.Where(u => u.Age > 12);
foreach (User user in selectedUsers)
Console.WriteLine($"{user.Name} - {user.Age}");
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
Ответ
Лера - 13
Мила - 14
using System;
List<User> users = new List<User>
{
new User {
Name="Каир",
Age=17,
},
new User {
Name="Костя",
Age=18,
},
new User {
Name="Джон",
Age=19,
},
new User {
Name="Саша",
Age=20,
}
};
var items = users.Select(u => new {
FirstName = u.Name,
DateOfBirth = DateTime.Now.Year - u.Age
});
foreach (var n in items)
Console.WriteLine($"{n.FirstName} - {n.DateOfBirth}");
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
Ответ
Каир - 2007
Костя - 2006
Джон - 2005
Саша - 2004
using System;
List<User> users = new List<User>()
{
new User {
Name="Катя",
Age=17,
},
new User {
Name="Маша",
Age=18,
},
new User {
Name="Даша",
Age=29,
},
new User {
Name="Кирилл",
Age=30,
},
};
List<Animals> places = new List<Animals>()
{
new Animals
{
Animal = "белка",
Price = 88,
},
new Animals
{
Animal = "кошка",
Price = 33,
},
new Animals
{
Animal = "собака",
Price = 123,
},
new Animals
{
Animal = "бегемот",
Price = 312,
},
new Animals
{
Animal = "жираф",
Price = 3124,
},
};
var sortedUsers = users.OrderBy(u => u.Name);
foreach (User u in sortedUsers)
Console.WriteLine(u.Name);
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
public class Animals
{
public string Animal { get; set; }
public int Price { get; set; }
}
Ответ
Даша
Катя
Кирилл
Маша
using System;
List<User> users = new List<User>()
{
new User {
Name="Кирилл",
Age=8,
},
new User {
Name="Катя",
Age=22,
},
new User {
Name="Камиль",
Age=88,
},
new User {
Name="Маша",
Age=90,
},
};
List<Animals> places = new List<Animals>()
{
new Animals
{
Mesto = "Змея",
Price = 2341,
},
new Animals
{
Mesto = "тараканы",
Price = 4576,
},
new Animals
{
Mesto = "жуки",
Price = 123,
},
new Animals
{
Mesto = "медведь",
Price = 122,
},
new Animals
{
Mesto = "кошка",
Price = 90,
},
};
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; }
}
public class Animals
{
public string Mesto { get; set; }
public int Price { get; set; }
}
Ответ
Камиль - 88
Катя - 22
Кирилл - 8
Маша - 90