我有一個記錄集合。其中有兩個拳擊手,比賽日期,位置等... 我想分開他們幾個月,並將他們組合在一起。目前我有下面的內容。它在一定程度上起作用。這在未來尋找匹配。那就是今年,並且逐步遍歷每個月(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());
}
}
}
看起來不錯,現在史蒂夫,但有一件事,你可能需要修改for循環「爲( int i = 0; i <= 12 ...「,這樣你會包含從現在到本月底的匹配 – 2010-08-24 22:56:31