I have a list of objects in which one of the properties of the object is a date string, if in JSON it would look like this below:
// this is a List<Measurements>
measurements: [
{
"Timestamp": "6/7/2016 10:25:27 AM",
"FactoryTimestamp": "6/7/2016 10:25:27 AM",
"ValueInMgPerDl": 265,
"TrendArrow": 4
},
{
"Timestamp": "6/7/2016 10:24:27 AM",
"FactoryTimestamp": "6/7/2016 10:24:27 AM",
"ValueInMgPerDl": 301,
"TrendArrow": 5
},
{
"Timestamp": "6/6/2016 11:24:27 PM",
"FactoryTimestamp": "6/6/2016 11:24:27 PM",
"ValueInMgPerDl": 102,
"TrendArrow": 2
},
{
"Timestamp": "6/6/2016 11:23:27 PM",
"FactoryTimestamp": "6/6/2016 11:23:27 PM",
"ValueInMgPerDl": 99,
"TrendArrow": 2
}
]
I'm trying to create a new list of objects out of this in which they are grouped by the Timestamp param with the hopes that I don't need to create a new type but can instead use a generic. This way the new list would look something like this:
measurementGroups [
{
"date": "6/7/2016 10:25:27 AM",
"measurementData" : [
{
"Timestamp": "6/7/2016 10:25:27 AM",
"FactoryTimestamp": "6/7/2016 10:25:27 AM",
"ValueInMgPerDl": 265,
"TrendArrow": 4
},
{
"Timestamp": "6/7/2016 10:24:27 AM",
"FactoryTimestamp": "6/7/2016 10:24:27 AM",
"ValueInMgPerDl": 301,
"TrendArrow": 5
}
]
},
{
"date": "6/6/2016 11:24:27 PM",
"measurementData" : [
{
"Timestamp": "6/6/2016 11:24:27 PM",
"FactoryTimestamp": "6/6/2016 11:24:27 PM",
"ValueInMgPerDl": 102,
"TrendArrow": 2
},
{
"Timestamp": "6/6/2016 11:23:27 PM",
"FactoryTimestamp": "6/6/2016 11:23:27 PM",
"ValueInMgPerDl": 99,
"TrendArrow": 2
}
}
]
I tried accomplishing this using Linq like I show below, but that that's only returning me a list of lists grouped by date. Am I able to achieve what I'm after with Linq or is there another approach I need to take?
// Current attempt using linq
var measurementGroupList = measurements
.GroupBy(d => Convert.ToDateTime(d.Timestamp).Date)
.Select(grp => grp.ToList())
.ToList();