c# - Is there a better solution to this List<DateTime> sorting? -


i writing class asp.net mvc menu. want able take list of blog entries , make archive menu similar this:

1/2011 (2) 3/2011 (1) 4/2011 (12) etc... 

here code using:

public class archivemenumodel {     private list<archivemenuitem> menuitems;     public list<archivemenuitem> menuitems { { return menuitems; } }      public archivemenumodel(list<datetime> dates, string streamurl)     {         menuitems = new list<archivemenuitem>();         int itemcount = 0;         dates = dates.orderbydescending(x => x).tolist();          (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 better way, perhaps using linq or something? specifically, part of code don't is:

if(!(dates[i].month == dates[i + 1].month && dates[i].year == dates[i + 1].year)) 

if can avoid such ugly if statement, great!

menuitems = dates      .groupby(x => new datetime(x.year, x.month, 1))      .select(x=> new{date = x.key, count = x.count()})      .orderbydescending(x => x.date)      .select(x => new archivemenuitem(streamurl,                                        x.date.month,                                        x.date.year,                                        x.count))      .tolist(); 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -