I am writing a class for an ASP.NET MVC menu. I want to be able to take a list of all blog entries and make an Archive menu similar to this:
1/2011 (2)
3/2011 (1)
4/2011 (12)
etc...
Here is the code that I am using:
public class ArchiveMenuModel
{
private List<ArchiveMenuItem> menuItems;
public List<ArchiveMenuItem> MenuItems { get { return menuItems; } }
public ArchiveMenuModel(List<DateTime> dates, string streamUrl)
{
menuItems = new List<ArchiveMenuItem>();
int itemCount = 0;
dates = dates.OrderByDescending(x => x).ToList();
for (int i=0; i<dates.Count; i++)
{
itemCount++;
if(i+1 < dates.Count)
{
if(!(dates[i].Month == dates[i + 1].Month && dates[i].Year == dates[i + 1].Year))
{
menuItems.Add(new ArchiveMenuItem(streamUrl, dates[i].Month, dates[i].Year, itemCount));
itemCount = 0;
}
}
else
{
menuItems.Add(new ArchiveMenuItem(streamUrl, dates[i].Month, dates[i].Year, itemCount));
}
}
}
}
Is there a better way, perhaps by using Linq or something? Specifically, the part of my code that i don't like is:
if(!(dates[i].Month == dates[i + 1].Month && dates[i].Year == dates[i + 1].Year))
If I can avoid such an ugly if statement, that would be great!