仅按开始日期排序日期时间

时间:2013-02-27 03:50:23

标签: c#

我已经问了一个关于Sorting Date Time的不同问题,并得到了另一个用户的帮助来传递我的值。我正在使用像下面这样的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循环。

2 个答案:

答案 0 :(得分:3)

你在循环之后创建breaks,你需要在循环之外这样做:

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);
List<Break> breaks = new List<Break>();
for (int i = 0; i < starts.Count; i++)
{
    breaks.Add(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);
}

答案 1 :(得分:2)

由于@Corylulu在基本问题上打败了我,这里有一个不同的方法,稍微短一些:

IEnumerable<Break> breaks = 
    starts.Zip(ends, (s, e) => new Break { MealStart = s, MealEnd = e })
    .OrderBy(b => b.MealStart);

foreach (Break brk in breaks)
    Console.WriteLine("Start: {0}\tEnd: {1}", brk.BreakStart, brk.BreakEnd);

IEnumerable.Zip方法接受一对IEnumerable s和转换函数,并生成一个输出IEnumerable,其中包含使用每个输入{{1}的成员调用转换函数的结果}。您可以在结束时将其转换为IEnumerableList<Break>