0

I am trying to create an Archive, with the number of items per month inside parenthesis, and I have come up with something like this:-

@{
  var startMonth = DateTime.Now.Month;
  var startYear = DateTime.Now.Year;
  var blogList = Model.BlogViewModel.BlogArchiveList;
  var monthName = DateTime.Now.ToString("MMMM");
  var Counter = 0;
}
@foreach (var item in blogList)
{
  if (item.BlogDate.Year == startYear)
  {
      if (item.BlogDate.Month == startMonth)
      {
          Counter = Counter + 1;
      }
      else
      {
          Counter = 1;
          startMonth = item.BlogDate.Month;
      }
  }
  else
  {
      Counter = 1;
      startYear = item.BlogDate.Year;
      startMonth = item.BlogDate.Month;
  }
  monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(startMonth);
  @Html.ActionLink(monthName + " " +  startYear + "("+ Counter +")", "Archive", new  
  {
     year=startYear, month=startMonth}) <br/>
  }

Although this seems to work, I am getting the month written multiple times if there is more than one entry.

How can I avoid writing the month multiple times, while still getting the count of posts for that month?

Thanks for your help and time

Hogan
  • 69,564
  • 10
  • 76
  • 117
JMon
  • 3,387
  • 16
  • 63
  • 102
  • reformatting with indenting I note that you are missing at least one `}`. Is that the next character or was there more to your code block? – Hogan Jan 09 '13 at 17:24
  • Oh... and please indent in the future -- you are asking for help, don't make our eyes bleed while we look at your code -- thanks! – Hogan Jan 09 '13 at 17:24
  • @Hogan sorry I left 1 } out. How do I indent? Its always a struggle to paste code in the code window – JMon Jan 09 '13 at 17:31
  • 1
    There is a help button in the editor that explains better than I could, but one way is to put spaces in, as long as a line starts with more than 4 spaces it will format it as code and then spaces matter. Click on the "edited x mins ago" above my name to see the changes I made -- that should help too. – Hogan Jan 09 '13 at 17:41
  • Cool thanks Hogan, I will do that for next time – JMon Jan 09 '13 at 17:46

1 Answers1

1

Try to use next code:

var statistics = 
     blogList.GroupBy(val => new {val.BlogDate.Month, val.BlogDate.Year})
             .OrderBy(item => item.Key.Year)
             .ThenBy(item => item.Key.Month)
             .Select (grouped => new {Month = grouped.Key.Month, Year = grouped.Key.Year, Count = grouped.Count() });

foreach (var item in statistics)
{
    var monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(item.Month);
    Console.WriteLine (string.Format("{0} {1} ({2})", monthName, item.Year, item.Count));   
}

Prints correct results, for example

December 2012 (3)
January 2013 (4)
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
  • Thanks Ilya that worked! Is it ok to include the "var statistics" inside the Partial View or is it better to pass it from the controller? – JMon Jan 09 '13 at 17:38
  • you should definitely pass it from controller. Your views should be as simple as possible and controllers too. Pretty much like everything in an application. But anyway - don't calculate statistics in view - it's hard to maintain and test. – Ilya Ivanov Jan 09 '13 at 17:39
  • How to do that? I can pass it inside a @ViewBag.statistics, however I have the PartialView embedded in about 3 pages, and I have an ActionResult inside the BlogController, but this is ever triggered. How can I trigger it? – JMon Jan 09 '13 at 17:43
  • See how should model be passed into partial view here http://stackoverflow.com/questions/8724847/asp-net-mvc-pass-partial-data-model-to-partial-view – Ilya Ivanov Jan 09 '13 at 17:46
  • Thanks Ilya. So I have to pre-populate the ViewModel with the statistics List first. – JMon Jan 09 '13 at 17:51
  • yes, note that when you declare statistics you don't actually execute query. The statistics query is only being executed, when you start to iterate over it, i.e. it is differed. – Ilya Ivanov Jan 09 '13 at 17:53
  • Cool! How can I convert the statistics to IEnumerable? So that I can pass this list to the view through the ViewModel – JMon Jan 09 '13 at 18:02
  • Getting an error Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) – JMon Jan 09 '13 at 18:26
  • Ok solved! Created a new class with only the Month, Year and Count and iterated on that. Thanks a lot Ilya! – JMon Jan 09 '13 at 18:40
  • You won't get month without post though – remi bourgarel Jan 10 '13 at 09:01
  • @remi, that is what I wanted to achieve, I do not need to display a month if there is no post in it – JMon Jan 10 '13 at 09:10