2013-03-07 91 views
1

替代值我有一個時隙爲這樣:選擇在陣列

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」值是「一」,那麼希望得到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] 

是有意義呢?

+0

'的for(int i = 1;我 2013-03-07 04:18:33

+2

你只想某種計算的預訂時間給定休息或給予BOOKINGS? (這是一個反轉邏輯)... 它應該總結所有在時隙之間/在時隙之間的預訂時間,對吧?你使用哪個.net版本? – TheHe 2013-03-07 04:26:20

+0

@Usher你能改寫你的問題嗎?你試圖達到的目標並不明確。 – joce 2013-03-07 04:35:13

回答

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

它不是完全清楚你正在嘗試做的。 這並沒有太大的意義:

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

但我懷疑,你所要做的是把重點放在列表中沒有自己的項目,但在連續對(項目1與項目2,項目2與項目3 Item3和Item4等)。實現這一

一種方法是用自身像這樣的偏移版本進行合併時隙:

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)); 
} 
+0

.zip文件是highskill-優雅;),但所產生的列表將包含3項,而不是兩個,因爲只有「左側」纔是shiftet ONE項目... – TheHe 2013-03-07 05:11:02