SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | using System.Linq; | |
4 | ||
5 | namespace LinqTrain | |
6 | { | |
7 | class Program | |
8 | { | |
9 | public static void Main() | |
10 | { | |
11 | List<Soldier> soldiers = new List<Soldier>(); | |
12 | Console.WriteLine("Вывод до проекции"); | |
13 | for (int i = 0; i < 10; i++) | |
14 | { | |
15 | soldiers.Add(new Soldier()); | |
16 | Console.WriteLine(soldiers[i].GetInfo()); | |
17 | } | |
18 | Console.ReadKey(); | |
19 | ||
20 | Console.Clear(); | |
21 | - | var sortList = soldiers.Select(soldier => new |
21 | + | var soldiersData = soldiers.Select(soldier => new |
22 | { | |
23 | Name = soldier.Name, | |
24 | Rank = soldier.Rank | |
25 | }); | |
26 | Console.WriteLine("Вывод после проекции"); | |
27 | - | foreach (var item in sortList) |
27 | + | foreach (var item in soldiersData) |
28 | { | |
29 | Console.WriteLine($"{item.Name} - {item.Rank}"); | |
30 | } | |
31 | Console.ReadKey(); | |
32 | } | |
33 | } | |
34 | public static class RandomStatic | |
35 | { | |
36 | static private Random _rand = new Random(); | |
37 | static public int GetNext(int min, int max) | |
38 | { | |
39 | return _rand.Next(min, max); | |
40 | } | |
41 | } | |
42 | public static class GeneratorName | |
43 | { | |
44 | public static string CreateName() | |
45 | { | |
46 | string result = ""; | |
47 | int length = RandomStatic.GetNext(3, 10); | |
48 | for (int i = 0; i < length; i++) | |
49 | { | |
50 | result += (char)RandomStatic.GetNext('A', 'Z' + 1); | |
51 | } | |
52 | return result; | |
53 | } | |
54 | } | |
55 | class Soldier | |
56 | { | |
57 | public string Name { get; private set; } | |
58 | public string Weapon { get; private set; } | |
59 | public string Rank { get; private set; } | |
60 | public int WorkTime { get; private set; } | |
61 | public Soldier() | |
62 | { | |
63 | Name = GeneratorName.CreateName(); | |
64 | Weapon = new string[] { "Пистолет", "Автомат", "Винтовка" }[RandomStatic.GetNext(0, 3)]; | |
65 | Rank = new string[] { "Рядовой", "Сержант", "Прапор" }[RandomStatic.GetNext(0, 3)]; | |
66 | WorkTime = RandomStatic.GetNext(0, 13); | |
67 | } | |
68 | public string GetInfo() | |
69 | { | |
70 | return $"{Name} - {Weapon} - {Rank} - {WorkTime} месяцев"; | |
71 | } | |
72 | } | |
73 | } |