1

I have this code, it failed because thisMonthSundays are empty:

 public ActionResult TradeUKKPISearchesData() //show dropdownlist in the view
{
    var now = DateTime.Now;
    var lastMonth = now.AddMonths(-1);
    var thisMonthSundays = GetDatesOfSundays(now.Year, now.Month).OrderByDescending(x => x.Date);
    var lastMonthSundays = GetDatesOfSundays(lastMonth.Year, lastMonth.Month).OrderByDescending(x => x.Date); //problem here, must add some sort of check here?
    var sundaysToTakeFromLastMonth = 4;
    var sundays = thisMonthSundays.Concat(lastMonthSundays.Skip(Math.Max(0, lastMonthSundays.Count() - sundaysToTakeFromLastMonth)).Take(sundaysToTakeFromLastMonth));

    var allSundaysInThisMonth = new SundaysInMonthViewModel
    {
        AllSundays = sundays.Select(x => new SelectListItem
        {
            Value = x.ToString("dd/MM/yyyy"),
            Text = x.ToString("dd/MM/yyyy"),
        })
    };

    var selectedSunday = new SundaysInMonthViewModel
    {
        SelectedSunday = thisMonthSundays.Where(x => x <= now).Last() //failed here
    };

  return View(allSundaysInThisMonth);
}

private IEnumerable<DateTime> GetDatesOfSundays(int year, int month)
{
  var ci = CultureInfo.InvariantCulture;
  for (int i=1; i <= ci.Calendar.GetDaysInMonth(year, month); i++)
  {
      var date = new DateTime(year, month, i);
      if ((date.DayOfWeek == DayOfWeek.Sunday) && (date <= DateTime.Now))
      {
          yield return date; //skips all for this month
      }
  }
}

I need to fix this, please help with ideas? thanks

charlie_cat
  • 1,830
  • 7
  • 44
  • 73
  • You should not use var everywhere. Consider this article http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp – Dmitrii Dovgopolyi Oct 01 '12 at 10:46
  • 1
    simple question: Did you run this code today? cause given your code, there is no Sunday on October :) – Samy Arous Oct 01 '12 at 10:49
  • yes there is no sunday in October yet, and yes I ran it today, so I must add a check to see if sundays in month = 5 and not 4 so I can get yesterday in my list, I read that if you want to count you IEnumerable, the only way is to iterate through it, there is no .count()... :( – charlie_cat Oct 01 '12 at 11:23

2 Answers2

3

As the Octobar month do not have SUnday so far, the variable SelectedSunday is empty....

You can use LastOrDefault() instead :

SelectedSunday = thisMonthSundays.Where(x => x <= now).LastOrDefault() ;

Note : The Default value for DateTime Type is DateTime.Min which is 1/1/0001 12:00:00 AM.

Akash KC
  • 16,057
  • 6
  • 39
  • 59
  • That wouldn't explain why thisMonthSundays is empty. It just return a default date which might not be the best solution here. – Samy Arous Oct 01 '12 at 10:51
  • @Charlie_cat : LastOrDefault() is helpful in empty collection souce which do not throw expception while collection is empty instead it gives default value of that type if the collection is empty..... – Akash KC Oct 01 '12 at 10:55
  • @lcfseth : It simply returns empty as this month(OCtobar) do not contain sunday so far......About LastOrDefault(), it's helpful in empty collection... – Akash KC Oct 01 '12 at 10:58
  • @LolCoder Not saying your answer is wrong :) Just stating that this doesn't explain the exception. Also, the default value for DateTime is DateTime.Min not null. You should add a condition check to your code IMAO. – Samy Arous Oct 01 '12 at 11:02
  • thanks all, i understand now..I must still fix my other problem..will see what I can do there. – charlie_cat Oct 01 '12 at 11:13
1

There are some mistakes in your code here.

  1. Using var is not something you want to do everywhere.
  2. You should never use arbitrary values in your functions. Instead of checking that the days are prior to today, you should add a limit parameter to your function and pass DateTime.Now on the call.
  3. Your function is already returning all the Sundays of a given month that are prior to today. Your Linq Request is just a replication of code and will return the whole collection every-time.
  4. Since today is 10-01 and that we are Monday, there is no Sundays on October prior to today. This is why your collection is empty.
Samy Arous
  • 6,794
  • 13
  • 20