Program.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //string[] words = { "cherry", "apple", "blueberry", "z", "cherry", "apple", "blueberry", "z" };
  13. //// words.GroupBy()
  14. //IEnumerable<IGrouping<string, string>> studentGroup = words.GroupBy(s => s);
  15. //foreach (IGrouping<string, string> word in studentGroup)
  16. //{
  17. // Console.WriteLine(word.ToString());
  18. // foreach (string value in word)
  19. // {
  20. // Console.WriteLine(value);
  21. // }
  22. //}
  23. //Console.WriteLine("--------------------");
  24. //int shortestWordLength = words.Min(s => s.Length);
  25. //Console.WriteLine(shortestWordLength);
  26. //foreach (string word in words)
  27. //{
  28. // Console.WriteLine(word);
  29. //}
  30. //Console.WriteLine("--------------------");
  31. //var temps = words.OrderBy(s=>s.Length);
  32. //foreach (string word in temps)
  33. //{
  34. // Console.WriteLine(word);
  35. //}
  36. //Console.WriteLine("--------------------");
  37. //List<int> intList = new List<int>();
  38. //intList.AddRange(new[] {8,2,6,3,4,5,7,8,9,5 });
  39. //foreach (int word in intList)
  40. //{
  41. // Console.WriteLine(word.ToString());
  42. //}
  43. //Console.WriteLine("--------------------");
  44. //var tempList = intList.OrderBy(s => s);
  45. //foreach (int word in tempList)
  46. //{
  47. // Console.WriteLine(word.ToString());
  48. //}
  49. //Console.WriteLine("---------分组-----------");
  50. List<myMode> myModeList = new List<myMode>();
  51. myModeList.Add(new myMode("一班", "张三", 12));
  52. myModeList.Add(new myMode("一班", "李四", 20));
  53. myModeList.Add(new myMode("二班", "王五", 3));
  54. myModeList.Add(new myMode("二班", "小明", 60));
  55. myModeList.Add(new myMode("二班", "小丽", 18));
  56. IEnumerable<IGrouping<string, myMode>> studentGroup = myModeList.GroupBy(s => s.ClassName);
  57. foreach (IGrouping<string, myMode> word in studentGroup)
  58. {
  59. Console.WriteLine(word.Key);
  60. foreach (myMode value in word)
  61. {
  62. Console.WriteLine(string.Format("班:{2}Name:{0},Age:{1}", value.Name, value.Age.ToString(), value.ClassName));
  63. }
  64. }
  65. //foreach (myMode m in myModeList)
  66. //{
  67. // Console.WriteLine(string.Format("Name:{0},Age:{1}",m.Name,m.Age.ToString()));
  68. //}
  69. // Console.WriteLine("--------排序------------");
  70. // var mys = myModeList.OrderBy(m => m.Age);
  71. //double gage= myModeList.Average(s => s.Age);
  72. // foreach (myMode m in mys)
  73. // {
  74. // Console.WriteLine(string.Format("Name:{0},Age:{1}", m.Name, m.Age.ToString()));
  75. // }
  76. // Console.WriteLine("--------查找------------");
  77. // IEnumerable<myMode> mModel = myModeList.Where(m => m.Age >12);//myModeList.Where(m=>m.Name=="李四");
  78. // foreach (myMode value in mModel)
  79. // {
  80. // Console.WriteLine(string.Format("Name:{0},Age:{1}", value.Name, value.Age.ToString()));
  81. // }
  82. // Console.WriteLine("---------将序列中的每个元素投影到新表中---分组统计--------");
  83. //var newList= myModeList.GroupBy(g => g.ClassName).Select(s=>(new{NewClass=s.Key,GroupCount=s.Count(),Name=s.Max(item=>item.Name), Average_Age = s.Average(item => item.Age) }));
  84. // foreach (var value in newList)
  85. // {
  86. // Console.WriteLine(string.Format("班:{2}Name:{0},Age:{1}", value.Name, value.Average_Age.ToString(), value.NewClass));
  87. // }
  88. Console.ReadKey();
  89. /*
  90. List<int> list = new List<int>();
  91. list.AddRange(new int[] { 1, 5, 10, 20, 33 });
  92. //使用Lambda表达式
  93. List<int> evenNumbers = list.FindAll(i => {
  94. bool ret = (i % 2) == 0;
  95. ret = ret == true && i > 10;
  96. return ret;
  97. });
  98. List<int> retList = new List<int>();
  99. foreach (int num in evenNumbers)
  100. {
  101. retList.Add(num);
  102. }
  103. int retIndex = list.FindIndex(1,2,i => ((i % 2) == 0));
  104. test((string s)=>{
  105. return "";
  106. });
  107. */
  108. }
  109. }
  110. class myMode
  111. {
  112. public myMode() {
  113. }
  114. public myMode(string c,string n,int a)
  115. {
  116. this.name = n;
  117. this.age = a;
  118. this.ClassName = c;
  119. }
  120. string _ClassName = "";
  121. string name = "";
  122. int age = 0;
  123. public string Name
  124. {
  125. get
  126. {
  127. return name;
  128. }
  129. set
  130. {
  131. name = value;
  132. }
  133. }
  134. public int Age
  135. {
  136. get
  137. {
  138. return age;
  139. }
  140. set
  141. {
  142. age = value;
  143. }
  144. }
  145. public string ClassName
  146. {
  147. get
  148. {
  149. return _ClassName;
  150. }
  151. set
  152. {
  153. _ClassName = value;
  154. }
  155. }
  156. }
  157. }