在数组中选择备用值

时间:2013-03-07 04:17:25

标签: c#

我有一个时间段:

TimeSpan Midnight = new TimeSpan(24, 0, 0);
List<DateTime> Timeslot = new List<DateTime>();
Timeslot.Add(BookingStart)
Timeslot.Add(BookingEnd)
Timeslot.Add(breakstart1)
Timeslot.Add(breakEnd1)
Timeslot.Add(breakstart2)
Timeslot.Add(breakEnd2)
Timeslot.Add(breakstart3)
Timeslot.Add(breakEnd3)

for (int i = 1; i <= Timeslot.Count - 1; i++)
{
    if (Timeslot[0] != Timeslot[1])
    {
        if ((Timeslot[i].TimeOfDay < Midnight) &&
            (dutyEnd.TimeOfDay >= Midnight))
        {
            BookedHours = Midnight - Timeslot[i].TimeOfDay;
            // if i value is one then i want get the value like
            // BookedHours = Midnight - Timeslot[i,End].TimeOfDay;
            // BookedHours = Midnight - Timeslot[breakEnd1].TimeOfDay;
        }
    }
}

我在这里尝试做的是,如果我的“i”值是“one”,那么想获得breakEnd1值。

让我在这里解释清楚

我有一个预订例如

预订开始于:18.00和预订结束@(第二天):7.00

我之间有三次休息,接下来是休息 (breakstart1)从20.00开始 (breakEnd1)结束于:21.00 (breakstart2):24.00 (breakEnd2):01.00 (breakstart3):03.00 (breakEnd3):04.00

现在我要做的是

if midnight is not null and timeslot[i,end]<midnight then
am calculating booked hours like = midnight-timeslot[i,end]

现在有意义吗?

2 个答案:

答案 0 :(得分:1)

我的解决方案......

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {

            var bookingStartsAndEnds = new List<DateTime>()
                                           {
                                               DateTime.Parse("1999-02-01 13:50:00"),
                                               DateTime.Parse("1999-02-03 13:50:00"),
                                               DateTime.Parse("1999-02-04 13:05:00"),
                                               DateTime.Parse("1999-02-04 13:15:00"),
                                           };

            var bookedHours = bookingStartsAndEnds
                // order by the date ascending
                .OrderBy(dt => dt)
                // select only the "firsts" of the tuples
                .Where((dt, i) => i%2 == 0)
                // select the first + the next
                .Select((dt, i) => Tuple.Create<DateTime, DateTime?>(dt, bookingStartsAndEnds.ElementAtOrDefault((i*2) + 1)))
                // filter not-matching-end (the list must contain even number of items only!)
                .Where(t => t.Item2 != null)
                // calculate the time-difference between end-date and start-date and get all hours
                .Select(t => (t.Item2.Value - t.Item1).TotalHours)
                // sum them up
                .Sum(); 

            Console.WriteLine("{0:0.00} hours dude! this will be expensive...", bookedHours);
            Console.Read();
        }
    }
}

答案 1 :(得分:-1)

目前还不清楚你要做什么。 这没有多大意义:

if (Timeslot[0] != Timeslot[1]);

但我怀疑你要做的是关注不是列表中的项目,而是关注连续的对(Item1与Item2,Item2与Item3,Item3与Item4等)。

实现这一目标的一种方法是将Timeslot与其自身的偏移版本合并,如下所示:

foreach (var timewindow in Timeslot.Zip(Timeslot.Skip(1),Tuple.Create))
{
     Console.WriteLine(String.Format("handling duration from {0} to {1}",timewindow.Item1,timewindow.Item2));
}