最近在看Lambda表达式的文章,所以写了个简单的Demo,Mark一下。
Person.cs
using System; namespace LambdaDemo { /// <summary> /// 人 /// </summary> public class Person { private DateTime birthday; /// <summary> /// 设置或获取姓名。 /// </summary> public string Name { get; protected set; } /// <summary> /// 获取生日。 /// </summary> public string Birthday { get { return this.birthday.ToString("yyyy年M月d日"); } } /// <summary> /// 获取周岁。 /// </summary> public int Age { get { int age = DateTime.Today.Year - birthday.Year - 1; if (DateTime.Today.Month > birthday.Month || (DateTime.Today.Month == birthday.Month && DateTime.Today.Day > birthday.Day)) { ++age; } if (age < 0)//出生当年为0岁。 { return 0; } return age; } } /// <summary> /// 获取虚岁。 /// </summary> public int AbsAge { get { return DateTime.Today.Year - birthday.Year; } } /// <summary> /// 实例化一个人类对象。 /// </summary> /// <param name="name">姓名。</param> /// <param name="birthday">生日。</param> public Person(string name, string birthday) { this.Name = name; this.birthday = DateTime.Parse(birthday); if (this.birthday > DateTime.Today)//比当前日期日期晚的一律取当前日期为生日。 { this.birthday = DateTime.Today; } } public override string ToString() { return string.Format("{0},{1}生人,周岁{2}岁,虚岁{3}岁。", this.Name, this.Birthday, this.Age, this.AbsAge); } } }
Company.cs
using System; using System.Collections.Generic; using System.Linq; namespace LambdaDemo { /// <summary> /// 公司 /// </summary> public class Company { private List<Person> employee; /// <summary> /// 实例化一个公司。 /// </summary> public Company() { employee = new List<Person>(); } /// <summary> /// 添加一个公司员工。 /// </summary> public void Add(Person item) { employee.Add(item); } /// <summary> /// 添加一批公司员工。 /// </summary> public void AddRange(IEnumerable<Person> collecttion) { employee.AddRange(collecttion); } /// <summary> /// 查找某人是否该公司员工。 /// </summary> public bool Contains(Person item) { return employee.Contains(item); } /// <summary> /// 清理所有的员工。 /// </summary> public void Clear() { employee.Clear(); } /// <summary> /// 移除某个员工。 /// </summary> public bool Remove(Person item) { return employee.Remove(item); } /// <summary> /// 移除所有满足特定条件的员工。 /// </summary> /// <param name="match">特定条件</param> public int RemoveAll(Predicate<Person> match) { return employee.RemoveAll(match); } /// <summary> /// 移除某个索引位置的员工。 /// </summary> /// <param name="index"></param> public void RemoveAt(int index) { employee.RemoveAt(index); } /// <summary> /// 确定是否存在满足特定条件的员工。 /// </summary> /// <param name="match">特定条件</param> public bool Exists(Predicate<Person> match) { return employee.Exists(match); } /// <summary> /// 将容量设为实际元素数目。 /// </summary> public void TrimExcess() { employee.TrimExcess(); } /// <summary> /// 计算员工某项int32值类型的属性的平均值。 /// </summary> /// <param name="selector">指定用作计算的属性</param> public int Average(Func<Person,int> selector) { return (int)employee.Average(selector); } /// <summary> /// 基于谓词筛选值序列。 /// </summary> public IEnumerable<Person> Where(Func<Person, bool> selector) { return employee.Where(selector); } /// <summary> /// 公司。 /// </summary> /// <param name="index">员工索引位置。</param> public Person this[int index] { get { return employee[index]; } set { employee[index] = value; } } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LambdaDemo { class Program { static void Main(string[] args) { //int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; //int numCount = numbers.Count(s => s % 2 == 1); //Console.WriteLine("奇数个数:"+numCount); //Person[] company = { new Person("张三", "1982-5-7"), new Person("李四", "1983-9-15"), new Person("王五", "1979-1-11"), new Person("孙丽", "1984-11-7"), new Person("钱海", "1979-12-3"), new Person("赵兵", "1986-7-2") }; //int avgAge = (int)company.Average(p => p.Age); //Console.WriteLine("公司员工的平均年龄是:" + avgAge); Company company = new Company(); company.AddRange(new Person[] { new Person("张三", "1982-5-7"), new Person("李四", "1983-9-15"), new Person("王五", "1979-1-11"), new Person("孙丽", "1984-11-7"), new Person("钱海", "1979-12-3"), new Person("赵兵", "1986-7-2") }); int avgAge = company.Average(p => p.Age); var personUpAvgAge = company.Where(p => p.Age >= avgAge); Console.WriteLine("公司里年龄达到平均年龄的员工有:"); foreach (var p in personUpAvgAge) { Console.WriteLine(p.ToString()); } Console.ReadKey(); } } }
参考文章:http://msdn.microsoft.com/zh-cn/library/bb397687.aspx