使用Linq動態(tài)屬性排序 使用反射:
調(diào)用: List<Employee> list = new List<Employee>(); list.Add(new Employee() { Name = "張三", Age = 21, Salary = 1800f, Job = "UI" }); list.Add(new Employee() { Name = "李四", Age = 25, Salary = 2000f, Job = "DBA" }); list.Add(new Employee() { Name = "王五", Age = 24, Salary = 2000f, Job = "UI" }); list.Add(new Employee() { Name = "李九", Age = 31, Salary = 2900f, Job = "DBA" }); list.Add(new Employee() { Name = "張一", Age = 21, Salary = 2100f, Job = "UI" }); list.Add(new Employee() { Name = "王三", Age = 32, Salary = 2100f, Job = "DBA" }); //按Age排序 list.OrderBy(DynamicLambda<Employee,int>("Age")); //按Salary排序 // list.OrderBy(DynamicLambda<Employee,float>("Salary")); list.ForEach(e => Console.WriteLine(e.Name + "\t" + e.Age + "\t" + e.Salary));
|
|