0

我有一个这样的时间段:

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]

现在有意义吗?

4

2 回答 2

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();
        }
    }
}
于 2013-03-07T05:09:49.060 回答
-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));
}
于 2013-03-07T04:50:21.383 回答