Chartjs-Change List<Object> into List<List<int>> using LINQ

0👍

Assuming right now you have a list of PressureRowT and you want an object PressureRowT with lists within it, you can use something like

var Timestamps = pressureRowTList.Select(pressureRowT => pressureRowT.TimeStamp).ToList();

for each of the fields and assign them all to one object.

0👍

The most efficient way would be to re-write your parsing code to build the correct structures as the files are read.

Barring that, the best thing to do is not use LINQ, but simply build the Lists needed. A for loop is more efficient than a foreach when processing a List:

var src = new List<PressureRowT>();

var srccount = src.Count;
var PR_TS = new List<DateTime>(srccount);
var PR_P1 = new List<int>(srccount);
var PR_P2 = new List<int>(srccount);
var PR_P3 = new List<int>(srccount);
var PR_P4 = new List<int>(srccount);
var PR_P5 = new List<int>(srccount);

for (int j1 = 0; j1 < srccount; ++j1) {
    PR_TS.Add(src[j1].Timestamp);
    PR_P1.Add(src[j1].P1);
    PR_P2.Add(src[j1].P2);
    PR_P3.Add(src[j1].P3);
    PR_P4.Add(src[j1].P4);
    PR_P5.Add(src[j1].P5);
}

var ans = new { Timestamp = PR_TS, P1 = PR_P1, P2 = PR_P2, P3 = PR_P3, P4 = PR_P4, P5 = PR_P5 };

0👍

I might use Aggregate to build up the result object:

IEnumerable<PressureRowT> values;
var resultSeed = new {
    Timestamp = new List<DateTime>(),
    P1 = new List<int>(),
    P2 = new List<int>(),
    P3 = new List<int>(),
    P4 = new List<int>(),
    P5 = new List<int>(),
};

return values.Aggregate(
    resultSeed,
    (result, value) => {
        result.Timestamp.Add(value.Timestamp);
        result.P1.Add(value.P1);
        result.P2.Add(value.P2);
        result.P3.Add(value.P3);
        result.P4.Add(value.P4);
        result.P5.Add(value.P5);
        return result;
    });

Leave a comment