Advertisement
mituri

[C#] LINQ

Oct 9th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. /*
  2. Zadanie musi zostać rozwiązane za pomocą LINQ.
  3. Stwórz tablicę cities – zawierającą zadeklarowane przykładowe miasta ( częsć miast małą literą).
  4. Wypisz na konsole wszystkie miasta , które mają długość większą niż 4 ( Stringa) , wypisz miasta
  5. jako upperCase.
  6. Zdefiniuj klasę City , która posiada getter , konstruktory przechowuje następujące informację:
  7. -name
  8. -country
  9. -population
  10. Zdefiniuj Listę , która zawiera co najmniej 10 miast. Wypisz na ekeran wszystkie miasta , które
  11. mają liczbę ludności większą niż 100 000.
  12. Zdefinuj klasę Person z następującymi danymi :
  13. firstName
  14. lastName
  15. Age
  16. Zdefinuj odpowiedni konstruktor.
  17. Stwórz listę osób zawierającą co najmniej 10 osób.
  18. 1) Wypisz wszystkie osoby.
  19. 2) Wypisz wszystkie osoby , które są starsze niż 20 lat.
  20. 3) Wypisz wszystkie osoby , które są młodsze niż 18 lat o imieniu
  21.  
  22. */
  23.  
  24.  
  25.  
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Threading.Tasks;
  31. using System.IO;
  32. using System.Collections;
  33.  
  34. public class Cities {
  35. public string city { get; set; }
  36. public string country { get; set; }
  37. public long population { get; set; }
  38.  
  39. Cities( string city, string country, long population) {
  40. this.city = city;
  41. this.country = country;
  42. this.population = population;
  43. }
  44. public Cities() {
  45. city = null;
  46. country = null;
  47. population = 0;
  48. }
  49.  
  50. public void show() {
  51. Console.WriteLine(city + " " + country + " " + population );
  52. }
  53. }
  54.  
  55. public class Person {
  56. public string firstName;
  57. public string lastName;
  58. public int age;
  59.  
  60. public Person(string firstName, string lastName, int age) {
  61. this.firstName = firstName;
  62. this.lastName = lastName;
  63. this.age = age;
  64. }
  65. public Person() {
  66. firstName = null;
  67. lastName = null;
  68. age = 0;
  69. }
  70.  
  71. public void showPerson() {
  72. Console.WriteLine(firstName + " " + lastName + " " + age);
  73. }
  74.  
  75. }
  76.  
  77.  
  78. namespace zad1{
  79. class Program{
  80. static void Main(string[] args){
  81.  
  82. string[] cities = {"lodz", "Leba", "Sopot", "Warszawa", "katowice" };
  83.  
  84. var citiesQuery =
  85. from city in cities
  86. let len = city.Length
  87. where (len > 4)
  88. select city;
  89.  
  90. var result = citiesQuery.Select(c => c.ToUpper());
  91. foreach (var item in result)
  92. Console.WriteLine("{0,1} ", item);
  93. //////////////////////////////////////
  94. Console.WriteLine();
  95.  
  96. List<Cities> miasta= new List<Cities>();
  97. miasta.Add(new Cities() {city = "Warszawa" , country = "Polska", population = 1500000 });
  98. miasta.Add(new Cities() {city = "Moskwa" , country = "Rosja", population = 500 });
  99. miasta.Add(new Cities() {city = "Londyn", country = "Anglia", population = 7000000});
  100. miasta.Add(new Cities() {city = "Berlin " , country = "Niemcy", population = 40000});
  101. miasta.Add(new Cities() {city = "Wilno" , country = "Litwa", population = 1200000 });
  102. miasta.Add(new Cities() {city = "Minsk" , country = "Bialorus", population = 300});
  103. miasta.Add(new Cities() {city = "Kijow", country = "Ukraina", population = 2000000});
  104. miasta.Add(new Cities() {city = "Praga" , country = "Czechy", population = 1300000});
  105. miasta.Add(new Cities() {city = "Madryt", country = "Hiszpania", population =3200 });
  106. miasta.Add(new Cities() {city = "Paryz", country = "Francja", population = 5300000});
  107.  
  108. IEnumerable<Cities> MiastoPowyzejStuTys =
  109. from miasto in miasta
  110. where miasto.population > 100000
  111. select miasto;
  112.  
  113. foreach (var item in MiastoPowyzejStuTys)
  114. item.show();
  115. ////////////////////////////////
  116. Console.WriteLine();
  117.  
  118. List<Person> osoby = new List<Person>(){
  119. new Person("daniel", "dembowski", 80),
  120. new Person("daniel", "wojciechowski", 60),
  121. new Person("adam", "sek", 7),
  122. new Person("michal", "fabisiak", 23),
  123. new Person("adrian", "michalik", 2),
  124. new Person("piotr","krwaczyk", 18),
  125. new Person("konrad", "rzepka", 12),
  126. new Person("kuba", "wojewodzki", 55),
  127. new Person("radoslaw", "majdan", 54),
  128. new Person("bronislaw","komorowski", 46)
  129. };
  130.  
  131. IEnumerable<Person> ziomeczki =
  132. from osoba in osoby
  133. select osoba;
  134. foreach (var osoba in ziomeczki)
  135. osoba.showPerson();
  136. Console.WriteLine();
  137.  
  138. IEnumerable<Person> ziomeczki2 = ziomeczki.Where(osoba => osoba.age > 20);
  139. foreach (var osoba in ziomeczki2)
  140. osoba.showPerson();
  141. Console.WriteLine();
  142.  
  143. IEnumerable<Person> ziomeczki3 = ziomeczki.Where(osoba => osoba.age < 18 & osoba.firstName == "adam");
  144. foreach (var osoba in ziomeczki3)
  145. osoba.showPerson();
  146. Console.WriteLine();
  147.  
  148. foreach (var osoba in ziomeczki)
  149. if(osoba.firstName == "michal")
  150. osoba.showPerson();
  151. Console.WriteLine();
  152.  
  153. if (ziomeczki.Any(osoba => osoba.firstName == "michal")) {
  154. Console.WriteLine("Istnieje jakis michal na liscie");
  155. }
  156.  
  157. Console.ReadKey();
  158. }
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement