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」の値が「1」の場合、そのbreakEnd1値を取得したいということです。

ここで少し明確に説明させてください

たとえば、1つの予約があります

予約開始:18.00、予約終了@(翌日):7.00

間に3つのブレークがあり、それらのブレークは次のとおりです(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 など) に焦点を当てることだと思います。

これを実現する 1 つの方法は、次のようにタイムスロットをそれ自体のオフセット バージョンとマージすることです。

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 に答える