我在网上找到这种方法:
Code
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
List<Person> people = new List<Person>();
people.Add(new Person { FirstName = "Moses",
LastName = "McRoses",
DateOfBirth = new DateTime(1801, 1, 23) });
people.Add(new Person { FirstName = "Timothy",
LastName = "Khouri",
DateOfBirth = new DateTime(1985, 6, 20) });
people.Add(new Person { FirstName = "Jonathan",
LastName = "Carter",
DateOfBirth = new DateTime(1984, 6, 12) });
var param = Expression.Parameter(typeof(Person), "person");
var mySortExpression = Expression.Lambda<Func<Person, object>>(Expression.Property(param, "DateOfBirth"), param);
Person[] sortedPeople = people.OrderBy(mySortExpression).ToArray();
可是在我的程序中,Person[] sortedPeople = people.OrderBy(mySortExpression).ToArray();
这一句编译错误,
错误 86 无法从用法中推导出方法“System.Linq.Enumerable.OrderBy<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)”的类型实参。请尝试显式指定类型实参。
请问怎么来实现排序?