我最近编写了一个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