Advertisement
Yurka21

linq6

Jun 2nd, 2021 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace ConsoleApp3
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. List<Soldier> soldiers1 = new List<Soldier>{
  13. new Soldier("Kola","Serjant","Ak-47",2),
  14. new Soldier("Basya","Maior","PM",6),
  15. new Soldier("Alex","Prapor","AK-74",4),
  16. new Soldier("Bova","Captain","PM",4) };
  17.  
  18. List<Soldier> soldiers2 = new List<Soldier>{
  19. new Soldier("Oola","Serjant","Ak-47",2),
  20. new Soldier("Aasya","Maior","PM",6),
  21. new Soldier("Llex","Prapor","AK-74",4),
  22. new Soldier("Oova","Captain","PM",4) };
  23.  
  24. var newSoldiers = soldiers1.Where(soldier => soldier.Name.ToUpper().StartsWith('B'));
  25.  
  26. foreach (var soldier in newSoldiers)
  27. {
  28. soldiers2.Add(soldier);
  29. }
  30.  
  31. foreach (var soldier in soldiers2)
  32. {
  33. Console.WriteLine(soldier.Name);
  34. }
  35.  
  36. }
  37. }
  38.  
  39. class Soldier
  40. {
  41. public string Name { get; private set; }
  42.  
  43. public string Title { get; private set; }
  44. public string Armament { get; private set; }
  45. public int ServiceLife { get; private set; }
  46.  
  47. public Soldier(string name, string title, string armament, int serviceLife)
  48. {
  49. Name = name;
  50. Title = title;
  51. Armament = armament;
  52. ServiceLife = serviceLife;
  53. }
  54.  
  55.  
  56. }
  57. }
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement