Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace PersonDemo
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<Person> persons = Person.GetPerson();
  14.  
  15. List<Fruit> fruits = Fruit.GetFruit();
  16.  
  17. //1. Show the persons with assets of over 50B dollars.
  18. var q1 =
  19. from p in persons
  20. where p.Asset > 50
  21. select p;
  22.  
  23. Console.WriteLine("Assets over 50B \n");
  24.  
  25. foreach (var p in q1)
  26. Console.WriteLine(p);
  27.  
  28. // 2.Show all non-US citizens.
  29. var q2 =
  30. from p in persons
  31. where p.Country != "US"
  32. select p;
  33.  
  34. Console.WriteLine("Non-US Citizen\n");
  35. foreach (var p in q2)
  36. Console.WriteLine(p);
  37.  
  38. // 3. Show all females from India.
  39. var q3 =
  40. from p in persons
  41. where p.Country == "India" && p.IsFemale
  42. select p.Name;
  43.  
  44. Console.WriteLine("Indian Female\n");
  45. foreach (var p in q3)
  46. Console.WriteLine(p);
  47.  
  48. // 4. Show all persons whose first name is less than five letters long.
  49. var q4 =
  50. from p in persons
  51. where p.Name.Split()[0].Length < 5
  52. select p;
  53.  
  54. Console.WriteLine("First name less than five letters\n");
  55. foreach (var p in q4)
  56. Console.WriteLine(p);
  57.  
  58. // 5. Sort the collection by assets.
  59. var q5 =
  60. from p in persons
  61. orderby p.Asset
  62. select p;
  63.  
  64. Console.WriteLine("Sorting by asset\n");
  65. foreach (var p in q5)
  66. Console.WriteLine(p.Name + "\t" + p.Asset);
  67.  
  68. // 6. Group the collection by country
  69. var q6 =
  70. from p in persons
  71. group p
  72. by p.Country;
  73.  
  74. Console.WriteLine("Grouping by country\n");
  75. foreach (var aGroup in q6)
  76. {
  77. Console.WriteLine("{0} ({1})",
  78. aGroup.Key, aGroup.Count());
  79. foreach (var item in aGroup)
  80. Console.WriteLine(" " + item);
  81. }
  82.  
  83. // 7. Sort the above grouping.
  84. var q7 =
  85. from p in persons
  86. orderby p.Country
  87. group p
  88. by p.Country;
  89.  
  90. Console.WriteLine("Sort by country\n");
  91. foreach (var aGroup in q7)
  92. {
  93. Console.WriteLine("{0} ({1})",
  94. aGroup.Key, aGroup.Count());
  95. foreach (var item in aGroup)
  96. Console.WriteLine(" " + item);
  97. }
  98.  
  99. // 8. Own queries
  100. var q8 =
  101. from p in persons
  102. where p.Country == "India" && p.Name.Split()[0].Length < 5
  103. select p;
  104.  
  105. Console.WriteLine("First name less than five letters\n");
  106. foreach (var p in q8)
  107. Console.WriteLine(p);
  108.  
  109. var q9 =
  110. from p in persons
  111. orderby p.Asset descending
  112. select p.Name;
  113.  
  114. Console.WriteLine("Sort descending by Asset\n");
  115. foreach (var p in q9)
  116. Console.WriteLine(p);
  117.  
  118.  
  119. //LINQ - METHOD SYNTAX
  120.  
  121. // 1. Show all non-US citizens
  122. var p1 = persons.Select(p => p);
  123.  
  124. p1 = persons.Where(p => p.Country != "US");
  125.  
  126. Console.WriteLine("All Non-US Citizen\n");
  127. foreach (var p in p1)
  128. Console.WriteLine(p);
  129.  
  130. // 2. Shows only the name of all US citizens
  131. var p2 = persons.Select(p => p);
  132.  
  133. p2 = persons.Where(p => p.Name == "US");
  134.  
  135. Console.WriteLine("Name of all US citizen\n");
  136. foreach (var p in p2)
  137. Console.WriteLine(p);
  138.  
  139. // 3. Show all females from India
  140. var p3 = persons.Select(p => p);
  141.  
  142. p3 = persons.Where(p => p.IsFemale && p.Country == "India");
  143.  
  144. Console.WriteLine("Name of all US citizen\n");
  145. foreach (var p in p3)
  146. Console.WriteLine(p);
  147.  
  148. // 4. Sort the collection by last name and then first name
  149. var p4 = persons.Select(p => p);
  150.  
  151. p4 = persons.OrderBy(p => p.Name.Split()[1]);
  152.  
  153. Console.WriteLine("Ordered names by last name first\n");
  154. foreach (var p in p4)
  155. Console.WriteLine(p);
  156.  
  157. // 5. Group the collection by gender
  158.  
  159. var p5 = persons.GroupBy(p => p.IsFemale);
  160.  
  161. Console.WriteLine("Grouped collection by Gender\n");
  162. foreach (var grp in p5)
  163. {
  164. Console.WriteLine($"{(grp.Key ? "Female" : "Male")} persons");
  165. foreach (var item in grp)
  166. {
  167. Console.WriteLine(item);
  168. }
  169. }
  170. Console.ReadLine();
  171.  
  172. // 6. The first longest word in the string array words
  173. var p6 = persons.Select(p => p.Name.Split()[0]);
  174. var word = p6.Max();
  175. Console.WriteLine("First Longest Word\n" + word);
  176.  
  177. Console.ReadLine();
  178.  
  179.  
  180. // 7. The first word with the most vowels
  181.  
  182. string[] words = "the quick brown fox jumps over the lazy dog".Split();
  183.  
  184.  
  185. string vowels = "aeiou";
  186. var p7 = words.Select(p => p);
  187.  
  188. Console.WriteLine("A word with most vowels\n");
  189. int x = 0;
  190. var abc = "";
  191. foreach (var item in p7)
  192. {
  193. if (item.Count(p => vowels.Contains(p)) > x)
  194. {
  195. x = item.Count(p => vowels.Contains(p));
  196. abc = item;
  197. }
  198.  
  199. }
  200. Console.WriteLine(abc);
  201. Console.ReadLine();
  202.  
  203. // 8. All the elements in second and third with no duplicates. Do not use the Distinct() method
  204. string[] second = "a b c e".Split();
  205. string[] third = "a c d e".Split();
  206.  
  207. var v8 = second.Union(third);
  208.  
  209. Console.WriteLine("All the elements in second and third with no duplicates\n");
  210. foreach (var item in v8)
  211. {
  212. Console.WriteLine(item);
  213. }
  214.  
  215. Console.ReadLine();
  216.  
  217. // 9. Join on persons and fruits. (You may use a mixed-query).
  218.  
  219. var p9 = from p in persons
  220. join f in fruits
  221. on p.Country equals f.Origin
  222. select new { Person = p.Name, Fruit = f.Name };
  223.  
  224. Console.WriteLine("\n\nInner join.");
  225. foreach (var item in p9)
  226. Console.WriteLine($"{item.Person + ":"}{item.Fruit}");
  227.  
  228. Console.ReadLine();
  229.  
  230.  
  231. // 10. Any other method that is not describe above.
  232.  
  233. var p10 = from p in persons
  234. from f in fruits
  235. select new { Person = p.Country, Fruit = f.Name };
  236.  
  237. Console.WriteLine("\n\nCross Join.");
  238. foreach (var item in p10)
  239. Console.WriteLine($"{item.Person + ":"}{item.Fruit}");
  240.  
  241.  
  242.  
  243. }
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement