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> studentGroup = words.GroupBy(s => s); //foreach (IGrouping 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 intList = new List(); //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 myModeList = new List(); 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> studentGroup = myModeList.GroupBy(s => s.ClassName); foreach (IGrouping 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 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 list = new List(); list.AddRange(new int[] { 1, 5, 10, 20, 33 }); //使用Lambda表达式 List evenNumbers = list.FindAll(i => { bool ret = (i % 2) == 0; ret = ret == true && i > 10; return ret; }); List retList = new List(); 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; } } } }