我已经问过一个关于排序日期时间的不同问题,并得到了另一个用户的帮助来传递我的值。我正在使用像下面这样的 for 循环,但这里肯定是错误的,因为代码将值一一带来,而不是排序。
public class Break
{
public DateTime MealStart { get; set; }
public DateTime MealEnd { get; set; }
}
我的主要课程
IList<DateTime> starts = new List<DateTime>();
IList<DateTime> ends = new List<DateTime>();
DateTime breakStart1 = new DateTime(2012, 02, 15, 12, 30, 00); // 15/02/12 12.30PM
DateTime breakEnd1 = new DateTime(2012, 02, 15, 13, 30, 00); // 15/02/12 01.30PM
DateTime breakStart2 = new DateTime(2012, 02, 15, 11, 00, 00); // 15/02/12 11.00AM
DateTime breakEnd2 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakStart3 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakEnd3 = new DateTime(2012, 02, 15, 01, 00, 00); // 15/02/12 01.00PM
starts.Add(breakStart1);
starts.Add(breakStart2);
starts.Add(breakStart3);
ends.Add(breakEnd1);
ends.Add(breakEnd2);
ends.Add(breakEnd3);
for (int i = 0; i < starts.Count; i++)
{
var breaks = new List<Break>()
{
//for (int j= 0; j<starts.Count; j++)
//{
new Break()
{
MealStart = starts[i],
MealEnd = ends[i]
}
// }
};
var ordered = breaks.OrderBy(s => s.MealStart);
foreach (var ord in ordered)
{
System.Console.WriteLine(ord.MealStart);
System.Console.WriteLine(ord.MealEnd);
}
}
我期待如下结果
breakStart1 = 15/02/12 11.00AM
breakEnd1= 15/02/12 12.00PM
breakStart2 = 15/02/12 12.00PM
breakEnd2= 15/02/12 01.00PM
breakStart3 = 15/02/12 12.30PM
breakEnd3= 15/02/12 01.30PM
但这不是因为 for 循环。