2013-03-01 85 views
-1

我有一些棘手的日期範圍功能,我需要。日期範圍的LINQ查詢?

時間表有兩件事...一個開始日期和時間。

結束日期可以通過做計​​算:

ResolveEndDate(DateTime start, double hours) 
    { 
     int days = (int)Math.Floor(hours/GetDayHours()); 

     DateTime dt = start.AddDays(days); 
    } 

這是因爲工作一天有N個小時,等等。

在此基礎上,我需要找到項目,其中:

  • 開始之前之後
  • 開始範圍和結束前,內內
  • 起始端和內內
  • 起始端和結束後

我怎麼能寫一個LINQ查詢來做到這一點?

鑑於這樣的事情:

public static IEnumerable<Project> GetProjectRange(IEnumerable<Project> projects, DateTime start, DateTime end) 
     { 
      return from p in projects 
        where p.Schedule.DateFrom.Value... 
        select p; 
     } 

另一個棘手的事情是單月日,年應予以考慮。時間不應該被考慮。

1個滿足所有4個條件的查詢。

我沒有寫這個查詢的麻煩,但我會這樣做的方式將是一個巨大的混亂。我希望有更好的方法來做日期範圍。

謝謝

+0

你的意思是指這4個標準中的任何一個或每個標準的查詢?第一和第四標準不一樣嗎? – 2013-03-01 18:50:03

+0

對不起,這是一個錯誤。此外,1查詢滿足所有4. – jmasterx 2013-03-01 18:51:32

+2

我會說實話,這是瘋了。在這些條件下......每一個項目。沒有?你想讓它們按條件排序嗎? – 2013-03-01 18:57:33

回答

1

這樣的事情?

return 
    from p in projects 
    where 
     (  p.Schedule.DateFrom.Value < start 
      && p.Schedule.DateTo.Value > end 
     ) || // 1. 
     (  p.Schedule.DateFrom.Value < start 
      && p.Schedule.DateTo.Value > start 
      && p.Schedule.DateTo.Value < end 
     ) || // 2.    
     (  p.Schedule.DateFrom.Value > start 
      && p.Schedule.DateFrom.Value < end 
      && p.Schedule.DateTo.Value > start 
      && p.Schedule.DateTo.Value < end 
     ) || // 3.   
     (  p.Schedule.DateFrom.Value > start 
      && p.Schedule.DateFrom.Value < end 
      && p.Schedule.DateTo.Value > end 
     ) // 4. 
    select p; 
+0

這可能會減少到2因爲原來的4例可以減少到2例。我認爲OP應該弄清楚他們是如何好奇的 – JRoughan 2013-03-01 19:10:06

+0

@JRoughan我確信它可以在邏輯上得到簡化 - 我只是提供了與他的四個案例匹配的4個子句。 – 2013-03-01 19:11:44

+0

這正是我所需要的,謝謝 – jmasterx 2013-03-01 19:26:44