0

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();
3
  • The groupBy is returning a 2 dimensional list of keys with a list of values for each key. To get the individual values you need to use two Select methods .Select(grp => grp).Select(item => ........ Commented Jun 8, 2016 at 16:47
  • In a typical situation I would be fine with the way the GroupBy is returning the data. In this case I need to have the data structured similar to the middle example. Commented Jun 8, 2016 at 16:49
  • @jdweng Could you provide an example of what you mean? Commented Jun 8, 2016 at 16:57

1 Answer 1

1

I assume that you have a class Measurements

public class Measurements
{
    public DateTime Timestamp { get; set; }
    public DateTime FactoryTimestamp { get; set; }
    public int ValueInMgPerDl { get; set; }
    public int TrendArrow { get; set; }
}

and var MeasurementsList = new List<Measurements>(){.....} and on top of that try following query

    var measurementGroupList= MeasurementsList.GroupBy(s => s.Timestamp).Select(grp => new { date = grp.Key, measurementData = grp.Select(s => s) }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.