Question

I'm using Linqpad to dump (display) a list of counts by year / month. Everything seems fine but for the life of me I can't get the sort order to display properly.

var h = from x in myTable 
where x.Date >= DateTime.Parse("1/1/2013") && x.myField.EndsWith("abc")
group x by new 
{
x.Date.Value.Year,
x.Date.Value.Month
} 
into g

select new 
{  
Year = g.Select(y=>y.Date.Value.Year).First(),
Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Select(n=>n.Date.Value.Month).First()),
Number = g.Count()
};

H.Dump().OrderByDescending(y=>y.Year).ThenBy(m=>DateTime.Parse(m.Month));

2014    January    843
2014    February   635
2014    March      957
2014    April       61
2013    May      1
2013    June      1257
2013    July      1243
2013    August     796
2013    September  341
2013    October    765
2013    November   609
2013    December   630

I'm trying to get the output to display

2014    April       61
2014    March      957
2014    February   635
2014    January    843
2013    December   630
2013    November   609 

...

I've also tried

H.Dump().OrderByDescending(y=>y.Year).ThenByDescending(m=>DateTime.Parse(m.Month));

But that doesn't seem to impact the month sort order at all for some reason. Any ideas on what I'm doing / understanding wrong? Thanks!


I tried to remove some complexity and changed my month projection to get the Month value.

Month = (g.Select(n=>n.Date.Value.Month).First())

and updated the dump

H.Dump().OrderBy(y=>y.Year).ThenByDescending(m=>m.Month);

And my return is the same

2014    1   843
2014    2   635
2014    3   957
2014    4   73
2013    5   1
2013    6   1257
2013    7   1243
2013    8   796
2013    9   341
2013    10  765
2013    11  609
2013    12  630
Was it helpful?

Solution 2

All you need is to sort by the grouping keys:

var h = from x in myTable 
where x.Date >= DateTime.Parse("1/1/2013") && x.myField.EndsWith("abc")
group x by new 
{
    x.Date.Value.Year,
    x.Date.Value.Month
} 
into g
orderby g.Key.Year descending, g.Key.Month descending
...

Also, you can use Year = g.Key.Year etc. in the select.

OTHER TIPS

Well, with DateTime.Parse based on the month, you've got a new DateTime, not a month. And as you don't give a year, the current year will be taken.

So for example, for March 2013 (or 2012), you'll get a DateTime corresponding to March 2014. And March 2014 will also give you March 2014.

So try to get the month Property of your Datetime !

.OrderByDescending(x => x.Year)
.ThenByDescending(x => DateTime.Parse(x.Month).Month);

By the way I would use

DateTime.ParseExact(x.Month, "MMMM", CultureInfo.CurrentCulture).Month

Here's what worked! Thanks.

var H = from x in myTable
where x.Date >= DateTime.Parse("1/1/2013") && x.myField.EndsWith("abc")      
group x by new 
{
x.Date.Value.Year,
x.Date.Value.Month
} 
into g
orderby g.Key.Year descending, g.Key.Month descending
select new 
{
Year = g.Key.Year,
Month = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(g.Key.Month),
Number = g.Count()
};

H.Dump();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top