2010-08-24 26 views
1

我有一個記錄集合。其中有兩個拳擊手,比賽日期,位置等... 我想分開他們幾個月,並將他們組合在一起。目前我有下面的內容。它在一定程度上起作用。這在未來尋找匹配。那就是今年,並且逐步遍歷每個月(1-12),並找到該日期範圍內的任何匹配項。將記錄分離爲mvc的個別月份

將它轉換成int的一個很好的字典,枚舉其中int是一個月,enumberable是匹配的集合當月

//Build the matches list by Months!!! 
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>(); 
for (int i = 1; i <= 12; i++) 
{ 
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == DateTime.Now.Year && 
         x.MatchDate.Value.Month == i && 
         !x.HasResult() && 
         x.MatchDate.Value > DateTime.Now); 
    if (MatchesOfMonth.Count() > 0) 
    { 
     summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray()); 
    } 
} 

問題是這樣的目前僅今年交易。我想反過來讓它適用於「未來6個月」,但這當然也必須在新的一年裏工作!

什麼是最好的/最乾淨的方式去做到這一點?

在此先感謝!

PS上一個側面說明我還沒有找到如何根本就DateTime.Now.Month.add(1)爲例(因爲我總是會從當前日期向前走!)

--- --COMPLETED CODE -----

//Build the matches list by Months!!! 
     var summarysDic = new Dictionary<string, IEnumerable<MatchSummary>>(); 
     for (int i = 1; i <= 12; i++) 
     { 
      var checkDate = DateTime.Now.AddMonths(i); 
      var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Month == checkDate.Month && 
           x.MatchDate.Value.Year == checkDate.Year && 
           !x.HasResult() && 
           x.MatchDate.Value > DateTime.Now); 
      if (MatchesOfMonth.Count() > 0) 
      { 
       var firstMatchDate = MatchesOfMonth.First().MatchDate.Value; 
       if (firstMatchDate.Year != DateTime.Now.Year) 
       { 
        summarysDic.Add(firstMatchDate.ToString("MMMM yyyy"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray()); 
       } 
       else 
       { 
        summarysDic.Add(firstMatchDate.ToString("MMMM"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray()); 
       } 

      } 
     } 
+0

看起來不錯,現在史蒂夫,但有一件事,你可能需要修改for循環「爲( int i = 0; i <= 12 ...「,這樣你會包含從現在到本月底的匹配 – 2010-08-24 22:56:31

回答

1

我相信你可以得到你想要的東西沒有顯著改變你的算法:

//Build the matches list by Months!!! 
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>(); 
for (int i = 0; i <= 6; i++) 
{ 
    var checkDate = DateTime.Now.AddMonths(i); 
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == checkDate.Year && 
         x.MatchDate.Value.Month == checkDate.Month && 
         !x.HasResult() && 
         x.MatchDate.Value > DateTime.Now); 
    if (MatchesOfMonth.Count() > 0) 
    { 
     summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray()); 
    } 
} 
+0

完美知道有一個簡單的方法待辦事項:) 我也擴展它,所以替換」int 「用一個字符串作爲每個組的標題,這將只是一個月(9月),如果今年或每年的一年,如果是另一個」2012年9月「 – Steve 2010-08-24 20:52:02

0

這有什麼錯DateTime.Now.AddMonth(1)

var MatchesOfMonth = matches.Where(x => x.MatchDate.Value <= DateTime.Now.AddMonth(i) 
            && !x.HasResult() 
            && x.MatchDate.Value > DateTime.Now); 

我沒有編譯的,但它應該只相當輕微tweeking運行...

+0

這將導致問題,因爲我想要所有匹配八月分組和所有比賽在九月分組。我不想將15月8日至9月15日分組在一起 – Steve 2010-08-24 20:31:44

相關問題