我有一个Employee对象列表.Employee Class可以如下:
Class Emplyoyee
{
public string name;
public string DateOFJoining; //Please note here date is in string format
}
Run Code Online (Sandbox Code Playgroud)
现在我想显示最新员工的员工名单,所以我使用下面的代码:
List<Employee> lst = GetEmployes();
lst = lst.OrderByDescending(x => x.DateOFJoining).ToList();
//Here binding to the repeater
rptEmp.DataSource = lst.Take(2);
rptEmp.DataBind();
Run Code Online (Sandbox Code Playgroud)
问题:这里的订货处理日期为字符串.但我想按日期订购.任何人都可以帮我这个吗?
提前致谢.
您需要在表达式中转换x.DateOfJoining为a .如果您对日期的质量有信心,可以尝试:DateTimeOrderByDescending
lst = lst.OrderByDescending(x => DateTime.Parse(x.DateOfJoining)).ToList();
Run Code Online (Sandbox Code Playgroud)