tags:

views:

28

answers:

2

so i have a collection of records. Which have two boxers, match date, location etc... I want to separate them by months and group them together. Currently I have what is below. And it works to a degree. That looks for matchdates in the future. that is this year and steps through each month (1-12) and finds any matches in that date range.

Placing it into a nice dictionary of int, enumerable where int is the month and enumberable is the collection of matches in that month

            //Build the matches list by Months!!!
        var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>();
        for (int i = 1; i <= 12; i++)
        {
            var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == DateTime.Now.Year &&
                                x.MatchDate.Value.Month == i &&
                                !x.HasResult() &&
                                x.MatchDate.Value > DateTime.Now);
            if (MatchesOfMonth.Count() > 0)
            {
                summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray());
            }
        }

Problem is this currently only deals with this year. I would like to instead make it so it works for "the next 6 months" but this would of course have to work over the new year as well!

Whats the best/cleanest way to go about doing this?

thanks in advance!

P.S on a side note i have yet to find how to simply do DateTime.Now.Month.add(1) for example (as i will always be going from current date forwards!)

-----COMPLETED CODE!-----

//Build the matches list by Months!!!
        var summarysDic = new Dictionary<string, IEnumerable<MatchSummary>>();
        for (int i = 1; i <= 12; i++)
        {
            var checkDate = DateTime.Now.AddMonths(i);
            var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Month == checkDate.Month &&
                                x.MatchDate.Value.Year == checkDate.Year &&
                                !x.HasResult() &&
                                x.MatchDate.Value > DateTime.Now);
            if (MatchesOfMonth.Count() > 0)
            {
                var firstMatchDate = MatchesOfMonth.First().MatchDate.Value;
                if (firstMatchDate.Year != DateTime.Now.Year)
                {
                    summarysDic.Add(firstMatchDate.ToString("MMMM yyyy"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray());
                }
                else
                {
                    summarysDic.Add(firstMatchDate.ToString("MMMM"), MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x => new MatchSummary(x)).ToArray());
                }

            }
        }
+1  A: 

I believe you can get what you want without modifying your algorithm significantly:

//Build the matches list by Months!!!
var summarysDic = new Dictionary<int, IEnumerable<MatchSummary>>();
for (int i = 0; i <= 6; i++)
{
    var checkDate = DateTime.Now.AddMonths(i);
    var MatchesOfMonth = matches.Where(x => x.MatchDate.Value.Year == checkDate.Year &&
                        x.MatchDate.Value.Month == checkDate.Month &&
                        !x.HasResult() &&
                        x.MatchDate.Value > DateTime.Now);
    if (MatchesOfMonth.Count() > 0)
    {
        summarysDic.Add(i, MatchesOfMonth.OrderBy(x => x.MatchDate).Select(x=> new MatchSummary(x)).ToArray());
    }
}
Patrick McDonald
perfect knew there was a simple way todo it :) I also extened it so replace the "int" with a string which will be used on the page as the header of each group which will be just a month (september) if this year or month year if another year "September 2012"
Steve
A: 

What's wrong with DateTime.Now.AddMonth(1)?

var MatchesOfMonth = matches.Where(x => x.MatchDate.Value <= DateTime.Now.AddMonth(i)
                                   && !x.HasResult() 
                                   && x.MatchDate.Value > DateTime.Now);

I haven't compiled that, but it should run with only fairly minor tweeking...

AllenG
this would cause issues as i want all matches in august grouped together and all matches in sept grouped. I dont want to have 15th august to 15th of sept to be grouped together
Steve