我最近編寫了一個LINQ查詢來獲取包含最近6個月的展示位置數量的Dictionary
。如何使這個LINQ查詢更清潔?
它返回月份字符串 - 十進制金額對的Dictionary
。
它似乎有點cirlify。你們中的任何一位LINQ大師能夠幫助我重構這一點,使其更清潔一些?
/// <summary>
/// Gets the last 6 months of Placement History totalled by Month
/// for all Agencies
/// </summary>
/// <returns></returns>
public Dictionary<string, decimal> getRecentPlacementHistory()
{
var placementHistoryByMonth = new Dictionary<string, decimal>();
using (DemoLinqDataContext db = new DemoLinqDataContext())
{
for (int i = 0; i < 6; i++)
{
Decimal monthTotal =
(from a in db.Accounts
where
(a.Date_Assigned.Value.Month == DateTime.Now.AddMonths(-i).Month &&
a.Date_Assigned.Value.Year == DateTime.Now.AddMonths(-i).Month)
select a.Amount_Assigned).Sum();
String currentMonth = DateTime.Now.AddMonths(-i).ToString("MMM");
placementHistoryByMonth.Add(currentMonth, monthTotal);
}
return placementHistoryByMonth;
}
}
selectedDate.Month.Year沒有很多意義...錯字? ;) – em70 2009-06-19 17:13:19
@Jon Skeet,我該如何處理月份是一月份的情況。一旦我們從當前日期減去幾個月後,年度將不再匹配,並且此方法將失敗。我試圖找出如何解決這個問題,雖然每個月都不匹配。 – 2009-06-19 17:22:03