using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main()
{
var students = new List<Student>()
{
new Student("john", 18),
new Student("cathy", 22),
new Student("tom", 20)
};
//定义输出函数
Action<Student> print = student => Console.WriteLine(string.Concat(student.Name, ":", student.Age));
//循环输出
Console.WriteLine("全部输出:");
students.ForEach(print);
Console.WriteLine("-------------------");
//条件输出
Console.WriteLine("输出年龄大于21的:");
students.FindAll(student => student.Age > 21).ForEach(print);
Console.WriteLine("-------------------");
//排序输出
Console.WriteLine("年龄从小到大排序后输出");
students.Sort((f1, f2) => f1.Age.CompareTo(f2.Age));
students.ForEach(print);
Console.WriteLine("-------------------");
//添加一个新学生
students.Add(new Student("jerry", 20));
//先进行分组,再进行投影
Console.WriteLine("先进行分组,再进行投影");
var result = students.GroupBy(x => x.Age).Select(x => string.Concat(x.Key, ":", x.Count()));
result.ToList().ForEach(x => Console.WriteLine(x.ToString()));//循环输出得到结果
Console.WriteLine("-------------------");
Console.Read();
}
public class Student
{
public Student(string name, int age)
{
this.Name = name;
this.Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
}
}
}