123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- //string[] words = { "cherry", "apple", "blueberry", "z", "cherry", "apple", "blueberry", "z" };
- //// words.GroupBy()
- //IEnumerable<IGrouping<string, string>> studentGroup = words.GroupBy(s => s);
- //foreach (IGrouping<string, string> word in studentGroup)
- //{
- // Console.WriteLine(word.ToString());
- // foreach (string value in word)
- // {
- // Console.WriteLine(value);
- // }
- //}
- //Console.WriteLine("--------------------");
- //int shortestWordLength = words.Min(s => s.Length);
- //Console.WriteLine(shortestWordLength);
- //foreach (string word in words)
- //{
- // Console.WriteLine(word);
- //}
- //Console.WriteLine("--------------------");
- //var temps = words.OrderBy(s=>s.Length);
- //foreach (string word in temps)
- //{
- // Console.WriteLine(word);
- //}
- //Console.WriteLine("--------------------");
- //List<int> intList = new List<int>();
- //intList.AddRange(new[] {8,2,6,3,4,5,7,8,9,5 });
- //foreach (int word in intList)
- //{
- // Console.WriteLine(word.ToString());
- //}
- //Console.WriteLine("--------------------");
- //var tempList = intList.OrderBy(s => s);
- //foreach (int word in tempList)
- //{
- // Console.WriteLine(word.ToString());
- //}
- //Console.WriteLine("---------分组-----------");
- List<myMode> myModeList = new List<myMode>();
- myModeList.Add(new myMode("一班", "张三", 12));
- myModeList.Add(new myMode("一班", "李四", 20));
- myModeList.Add(new myMode("二班", "王五", 3));
- myModeList.Add(new myMode("二班", "小明", 60));
- myModeList.Add(new myMode("二班", "小丽", 18));
- IEnumerable<IGrouping<string, myMode>> studentGroup = myModeList.GroupBy(s => s.ClassName);
- foreach (IGrouping<string, myMode> word in studentGroup)
- {
- Console.WriteLine(word.Key);
- foreach (myMode value in word)
- {
- Console.WriteLine(string.Format("班:{2}Name:{0},Age:{1}", value.Name, value.Age.ToString(), value.ClassName));
- }
- }
- //foreach (myMode m in myModeList)
- //{
- // Console.WriteLine(string.Format("Name:{0},Age:{1}",m.Name,m.Age.ToString()));
- //}
- // Console.WriteLine("--------排序------------");
- // var mys = myModeList.OrderBy(m => m.Age);
- //double gage= myModeList.Average(s => s.Age);
- // foreach (myMode m in mys)
- // {
- // Console.WriteLine(string.Format("Name:{0},Age:{1}", m.Name, m.Age.ToString()));
- // }
- // Console.WriteLine("--------查找------------");
- // IEnumerable<myMode> mModel = myModeList.Where(m => m.Age >12);//myModeList.Where(m=>m.Name=="李四");
- // foreach (myMode value in mModel)
- // {
- // Console.WriteLine(string.Format("Name:{0},Age:{1}", value.Name, value.Age.ToString()));
- // }
- // Console.WriteLine("---------将序列中的每个元素投影到新表中---分组统计--------");
- //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) }));
- // foreach (var value in newList)
- // {
- // Console.WriteLine(string.Format("班:{2}Name:{0},Age:{1}", value.Name, value.Average_Age.ToString(), value.NewClass));
- // }
- Console.ReadKey();
- /*
-
- List<int> list = new List<int>();
- list.AddRange(new int[] { 1, 5, 10, 20, 33 });
- //使用Lambda表达式
- List<int> evenNumbers = list.FindAll(i => {
- bool ret = (i % 2) == 0;
- ret = ret == true && i > 10;
- return ret;
- });
- List<int> retList = new List<int>();
- foreach (int num in evenNumbers)
- {
- retList.Add(num);
- }
- int retIndex = list.FindIndex(1,2,i => ((i % 2) == 0));
- test((string s)=>{
- return "";
- });
- */
- }
- }
- class myMode
- {
- public myMode() {
- }
- public myMode(string c,string n,int a)
- {
- this.name = n;
- this.age = a;
- this.ClassName = c;
- }
- string _ClassName = "";
- string name = "";
- int age = 0;
- public string Name
- {
- get
- {
- return name;
- }
- set
- {
- name = value;
- }
- }
- public int Age
- {
- get
- {
- return age;
- }
- set
- {
- age = value;
- }
- }
- public string ClassName
- {
- get
- {
- return _ClassName;
- }
- set
- {
- _ClassName = value;
- }
- }
-
- }
- }
|