SHOW:
|
|
- or go back to the newest paste.
| 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | - | namespace C_sharp_Light |
| 3 | + | using System.Linq; |
| 4 | ||
| 5 | namespace LinqTrain | |
| 6 | {
| |
| 7 | - | static void Main(string[] args) |
| 7 | + | |
| 8 | {
| |
| 9 | - | |
| 9 | + | public static void Main() |
| 10 | {
| |
| 11 | List<Soldier> Squad1 = new List<Soldier>(); | |
| 12 | List<Soldier> Squad2 = new List<Soldier>(); | |
| 13 | Console.WriteLine("До передачи");
| |
| 14 | for (int i = 0; i < 10; i++) | |
| 15 | {
| |
| 16 | Squad1.Add(new Soldier()); | |
| 17 | Squad2.Add(new Soldier()); | |
| 18 | Console.WriteLine(Squad1[i].Surname + " " + Squad2[i].Surname); | |
| 19 | } | |
| 20 | Console.ReadKey(); | |
| 21 | ||
| 22 | Console.Clear(); | |
| 23 | List<Soldier> soldierForTransfer = Squad1.Where(solider => solider.Surname.ToUpper().StartsWith("Б")).ToList<Soldier>();
| |
| 24 | for (int i = 0; i < Squad1.Count; i++) | |
| 25 | {
| |
| 26 | foreach (var sol in soldierForTransfer) | |
| 27 | {
| |
| 28 | if (Squad1[i] == sol) | |
| 29 | {
| |
| 30 | Squad1.RemoveAt(i); | |
| 31 | i = 0; | |
| 32 | } | |
| 33 | } | |
| 34 | } | |
| 35 | Squad2 = Squad2.Union(soldierForTransfer).ToList<Soldier>(); | |
| 36 | Console.WriteLine("После передачи");
| |
| 37 | for (int i = 0; i < Squad2.Count; i++) | |
| 38 | {
| |
| 39 | if (i < Squad1.Count) | |
| 40 | Console.WriteLine(Squad1[i].Surname + " " + Squad2[i].Surname); | |
| 41 | else | |
| 42 | Console.WriteLine(" " + Squad2[i].Surname);
| |
| 43 | } | |
| 44 | Console.ReadKey(); | |
| 45 | } | |
| 46 | } | |
| 47 | public static class RandomStatic | |
| 48 | {
| |
| 49 | static private Random _rand = new Random(); | |
| 50 | static public int GetNext(int min, int max) | |
| 51 | {
| |
| 52 | return _rand.Next(min, max); | |
| 53 | } | |
| 54 | } | |
| 55 | class Soldier | |
| 56 | {
| |
| 57 | public string Surname { get; private set; }
| |
| 58 | public Soldier() | |
| 59 | {
| |
| 60 | Surname = new string[] { "Барбарис", "Антонио", "Вано" }[RandomStatic.GetNext(0, 3)];
| |
| 61 | } | |
| 62 | } | |
| 63 | } |