0

If I have a list of DateTimes or an object composed of Date Parts say:

public class CustomDate
{
    public string Month;
    public string Day;
    public DateTime Date;
}

var cusDate = List<CustomDate>();

is it possible to take it and group the days by month to get:

June 2, 9, 12, 20  May 9, 15, 20, 25 etc ...

How do I select the date fields out of the following:

var r = dt.OrderBy(a => a.Date).GroupBy(b => b.Month).Select(a => new { a.Key});

Edit:

Final Solution to get proper formatting:

var il = String.Empty;
var groupped = dt.OrderBy(x => x.Date).GroupBy(x => x.Month);
foreach (var item in groupped)
{
   il += String.Format("{0}", item.Key) + " ";
   foreach (var cd in item)
   {
       il += String.Format("{0}", cd.Day) + ", ";
   }
   il = il.Trim().TrimEnd(',') + " | ";
}
bumble_bee_tuna
  • 3,533
  • 7
  • 43
  • 83

2 Answers2

0

I believe you just need to loop individual item returned by GroupBy.

var groupped = cusDate.OrderBy(x => x.Date)
    .GroupBy(x => x.Month);
foreach (var item in groupped)
{
    Console.WriteLine("Key:{0}", item.Key);
    foreach (var cd in item)
    {
        Console.WriteLine("Date:{0}, Day:{1}, Month:{2}", cd.Date, cd.Day, cd.Month);
    }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
-1

Lets try with this , will solve your issue

    var query = cusDate.GroupBy(d=> d.Month)
                      .Select(group => 
                            new { Month= group.Key,
                                  CustomDate = group.OrderByAscending(x => x.Day) })
                      .OrderBy(group => group.CustomDate.First().Day);
MSTdev
  • 4,507
  • 2
  • 23
  • 40