-1

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

4

2 回答 2

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);
}
于 2013-02-27T03:55:27.343 回答
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方法采用一对IEnumerables 和一个变换函数,并生成一个输出IEnumerable,其中包含使用每个输入的成员调用变换函数的结果IEnumerable。当然,您可以在最后将其转换为List<Break>a ToList()

于 2013-02-27T04:04:44.257 回答