Search code examples
c#linqsorting

Using .ThenBy in a Linq causes inner object to be null


I am ingesting a response from the Bureau of Labor Statistics. I can grab a maximum of 20 years of data at a time. The response returns the last year and last month first and then descends from there.

I am trying to sort by year and month ascending.

Where year = a 4 digit year. Period = the letter M followed by a digit designating the month, for example January = M01.

Example Sorting

  • 1948 M01
  • 1948 M02
  • 1948 M03
  • .... Etc

below are the object(s).


  public class BLSResponse
    {
        public string status { get; set; }
        public int responseTime { get; set; }
        public List<string> message { get; set; }
        public Results Results { get; set; }
    }

    public class Results
    {
        public List<Series> series { get; set; }
    }

    public class Series
    {
        public string seriesID { get; set; }
        public Catalog catalog { get; set; }
        public List<Datum> data { get; set; }
    }

    public class Catalog
    {
        public string series_title { get; set; }
        public string series_id { get; set; }
        public string seasonality { get; set; }
        public string survey_name { get; set; }
        public string survey_abbreviation { get; set; }
        public string measure_data_type { get; set; }
        public string commerce_industry { get; set; }
        public string occupation { get; set; }
        public string cps_labor_force_status { get; set; }
        public string demographic_age { get; set; }
        public string demographic_ethnic_origin { get; set; }
        public string demographic_race { get; set; }
        public string demographic_gender { get; set; }
        public string demographic_education { get; set; }
    }

    public class Datum
    {
        public string year { get; set; }
        public string period { get; set; }
        public string periodName { get; set; }
        public string latest { get; set; }
        public string value { get; set; }
        public List<Footnote> footnotes { get; set; }
        public Calculations calculations { get; set; }
    }

    public class Calculations
    {
        public Net_Changes net_changes { get; set; }
        public Pct_Changes pct_changes { get; set; }
    }

    public class Net_Changes
    {
        [JsonProperty("1")]
        public string _1 { get; set; }
        [JsonProperty("3")]
        public string _3 { get; set; }
        [JsonProperty("6")]
        public string _6 { get; set; }
        [JsonProperty("12")]
        public string _12 { get; set; }
    }

    public class Pct_Changes
    {
        [JsonProperty("1")]
        public string _1 { get; set; }
        [JsonProperty("3")]
        public string _3 { get; set; }
        [JsonProperty("6")]
        public string _6 { get; set; }
        [JsonProperty("12")]
        public string _12 { get; set; }
    }

    public class Footnote
    {
        public string code { get; set; }
        public string text { get; set; }
    }

The below sort, sorts them by year just fine.

blsResponse.Results.series.ForEach(x => x.data = x.data.OrderBy(x => x.year).ToList());

however if I try any of the below, then all of the sudden the calculations object that is apart of the data is now null.

1

blsResponse.Results.series.ForEach(x => x.data = (x.data.OrderBy(y => y.year).ThenBy(p => p.period)).ToList());

2

blsResponse.Results.series.ForEach(x => x.data = x.data.OrderBy(x => x.year + x.period).ToList());

Here is an example JSON response for 1948 - 1949

{
    "status": "REQUEST_SUCCEEDED",
    "responseTime": 171,
    "message": [],
    "Results": {
        "series": [
            {
                "seriesID": "LNS11000000",
                "catalog": {
                    "series_title": "(Seas) Civilian Labor Force Level",
                    "series_id": "LNS11000000",
                    "seasonality": "Seasonally Adjusted",
                    "survey_name": "Labor Force Statistics from the Current Population Survey",
                    "survey_abbreviation": "LN",
                    "measure_data_type": "Number in thousands",
                    "commerce_industry": "All Industries",
                    "occupation": "All Occupations",
                    "cps_labor_force_status": "Civilian labor force",
                    "demographic_age": "16 years and over",
                    "demographic_ethnic_origin": "All Origins",
                    "demographic_race": "All Races",
                    "demographic_gender": "Both Sexes",
                    "demographic_education": "All educational levels"
                },
                "data": [
                    {
                        "year": "1949",
                        "period": "M12",
                        "periodName": "December",
                        "value": "61908",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-97",
                                "3": "275",
                                "6": "960",
                                "12": "739"
                            },
                            "pct_changes": {
                                "1": "-0.2",
                                "3": "0.4",
                                "6": "1.6",
                                "12": "1.2"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M11",
                        "periodName": "November",
                        "value": "62005",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-180",
                                "3": "415",
                                "6": "746",
                                "12": "1303"
                            },
                            "pct_changes": {
                                "1": "-0.3",
                                "3": "0.7",
                                "6": "1.2",
                                "12": "2.1"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M10",
                        "periodName": "October",
                        "value": "62185",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "552",
                                "3": "884",
                                "6": "1178",
                                "12": "1539"
                            },
                            "pct_changes": {
                                "1": "0.9",
                                "3": "1.4",
                                "6": "1.9",
                                "12": "2.5"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M09",
                        "periodName": "September",
                        "value": "61633",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "43",
                                "3": "685",
                                "6": "560",
                                "12": "818"
                            },
                            "pct_changes": {
                                "1": "0.1",
                                "3": "1.1",
                                "6": "0.9",
                                "12": "1.3"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M08",
                        "periodName": "August",
                        "value": "61590",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "289",
                                "3": "331",
                                "6": "533",
                                "12": "784"
                            },
                            "pct_changes": {
                                "1": "0.5",
                                "3": "0.5",
                                "6": "0.9",
                                "12": "1.3"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M07",
                        "periodName": "July",
                        "value": "61301",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "353",
                                "3": "294",
                                "6": "530",
                                "12": "120"
                            },
                            "pct_changes": {
                                "1": "0.6",
                                "3": "0.5",
                                "6": "0.9",
                                "12": "0.2"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M06",
                        "periodName": "June",
                        "value": "60948",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-311",
                                "3": "-125",
                                "6": "-221",
                                "12": "-9"
                            },
                            "pct_changes": {
                                "1": "-0.5",
                                "3": "-0.2",
                                "6": "-0.4",
                                "12": "0.0"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M05",
                        "periodName": "May",
                        "value": "61259",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "252",
                                "3": "202",
                                "6": "557",
                                "12": "1287"
                            },
                            "pct_changes": {
                                "1": "0.4",
                                "3": "0.3",
                                "6": "0.9",
                                "12": "2.1"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M04",
                        "periodName": "April",
                        "value": "61007",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-66",
                                "3": "236",
                                "6": "361",
                                "12": "330"
                            },
                            "pct_changes": {
                                "1": "-0.1",
                                "3": "0.4",
                                "6": "0.6",
                                "12": "0.5"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M03",
                        "periodName": "March",
                        "value": "61073",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "16",
                                "3": "-96",
                                "6": "258",
                                "12": "1003"
                            },
                            "pct_changes": {
                                "1": "0.0",
                                "3": "-0.2",
                                "6": "0.4",
                                "12": "1.7"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M02",
                        "periodName": "February",
                        "value": "61057",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "286",
                                "3": "355",
                                "6": "251",
                                "12": "533"
                            },
                            "pct_changes": {
                                "1": "0.5",
                                "3": "0.6",
                                "6": "0.4",
                                "12": "0.9"
                            }
                        }
                    },
                    {
                        "year": "1949",
                        "period": "M01",
                        "periodName": "January",
                        "value": "60771",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-398",
                                "3": "125",
                                "6": "-410",
                                "12": "676"
                            },
                            "pct_changes": {
                                "1": "-0.7",
                                "3": "0.2",
                                "6": "-0.7",
                                "12": "1.1"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M12",
                        "periodName": "December",
                        "value": "61169",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "467",
                                "3": "354",
                                "6": "212"
                            },
                            "pct_changes": {
                                "1": "0.8",
                                "3": "0.6",
                                "6": "0.3"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M11",
                        "periodName": "November",
                        "value": "60702",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "56",
                                "3": "-104",
                                "6": "730"
                            },
                            "pct_changes": {
                                "1": "0.1",
                                "3": "-0.2",
                                "6": "1.2"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M10",
                        "periodName": "October",
                        "value": "60646",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-169",
                                "3": "-535",
                                "6": "-31"
                            },
                            "pct_changes": {
                                "1": "-0.3",
                                "3": "-0.9",
                                "6": "-0.1"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M09",
                        "periodName": "September",
                        "value": "60815",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "9",
                                "3": "-142",
                                "6": "745"
                            },
                            "pct_changes": {
                                "1": "0.0",
                                "3": "-0.2",
                                "6": "1.2"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M08",
                        "periodName": "August",
                        "value": "60806",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-375",
                                "3": "834",
                                "6": "282"
                            },
                            "pct_changes": {
                                "1": "-0.6",
                                "3": "1.4",
                                "6": "0.5"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M07",
                        "periodName": "July",
                        "value": "61181",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "224",
                                "3": "504",
                                "6": "1086"
                            },
                            "pct_changes": {
                                "1": "0.4",
                                "3": "0.8",
                                "6": "1.8"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M06",
                        "periodName": "June",
                        "value": "60957",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "985",
                                "3": "887"
                            },
                            "pct_changes": {
                                "1": "1.6",
                                "3": "1.5"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M05",
                        "periodName": "May",
                        "value": "59972",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-705",
                                "3": "-552"
                            },
                            "pct_changes": {
                                "1": "-1.2",
                                "3": "-0.9"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M04",
                        "periodName": "April",
                        "value": "60677",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "607",
                                "3": "582"
                            },
                            "pct_changes": {
                                "1": "1.0",
                                "3": "1.0"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M03",
                        "periodName": "March",
                        "value": "60070",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "-454"
                            },
                            "pct_changes": {
                                "1": "-0.8"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M02",
                        "periodName": "February",
                        "value": "60524",
                        "footnotes": [
                            {}
                        ],
                        "calculations": {
                            "net_changes": {
                                "1": "429"
                            },
                            "pct_changes": {
                                "1": "0.7"
                            }
                        }
                    },
                    {
                        "year": "1948",
                        "period": "M01",
                        "periodName": "January",
                        "value": "60095",
                        "footnotes": [
                            {}
                        ]
                    }
                ]
            }
        ]
    }
}

I also tried sorting by period and then grouping by year and none of that worked either.

What am I missing here? I feel like if I can OrderBy I should be able to OrderBy.ThenBy, correct?

Thanks,

Adam


Solution

  • The provided data does not contain for calculations property in JSON for year 1948 and M01 period and after sorting this record becomes first (was last in the original data). The reordering does not affect the Datum instances itself.