How do you convert a List of object to a one dimensional array?

ankur

I have a list of objects which look like this:

 public class Hub
    {
        public string Stamp { get; set; }
        public string Latency0 { get; set; }
        public string Latency1 { get; set; }
        public string Latency2 { get; set; }
        public string Latency3 { get; set; }
        public string Latency4 { get; set; }
    }

After I convert this list into a Json it looks like the image below.

enter image description here

How can I convert the list into the array shown in the image? Either I should be able to create a C# array which I can further convert into a Json array shown in the image.

enter image description here

I tried using this ToArray() on the list but it only converts it into an array of object.

Konamiman

Aomine's answer is fine if you are fine with keeping your values as strings. However, your screenshot seems to suggest that you actually need these values converted to numbers. Since these can have decimals and can be null, decimal? is the type you need for that.

Start by creating this auxiliary method:

decimal? ParseOrNull(string value)
{
    decimal numericValue;
    return decimal.TryParse(value, out numericValue) ? numericValue : (decimal?)null;
}

And then:

hubs.Select(h => 
    new [] { h.Stamp, h.Latency0, h.Latency1, h.Latency2, h.Latency3, h.Latency4 }
            .Select(ParseOrNull).ToArray())
    .ToArray()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert List<List<Object>> to a multi-dimensional array?

How do I convert an array object into a two-dimensional array

Convert one dimensional point list in two dimensional np array

How to convert a pandas dataframe into one dimensional array?

How do you rotate a two dimensional array?

How do you convert an array-object into a plain array?

How to convert two dimensional array to one dimensional in PHP?

How to convert List of arrays to two dimensional array?

How do I convert a tuple of tuples to a one-dimensional list using list comprehension?

How do you convert one element in a list into many elements?

How do you convert an array to a nested object in javascript?

How do I convert an object array to an object list

convert one dimensional array to multidimesional

How do I convert array of Objects into one Object in JavaScript?

How do I convert array of Objects to one Object in JavaScript?

How can you convert a 2 dimensional array into a 1 dimensional array in Java

Convert 2 Dimensional Array into Object

How to convert a list to a one dimensional array (vector) in Common Lisp having the same output either using make-array or coerce?

how do I convert an object to an array list in JS

how to convert a one-dimensional array into multimers with a denamic step

How to convert two dimensional array of string into one of type int?

How do you get the width and height of a multi-dimensional array?

How do you pass a multi dimensional array from view to controller?

How to convert a two-dimensional boolean array to a one-dimensional byte array in java?

How efficiently to convert one dimensional array to two dimensional array in swift3

How to convert custom class list to two dimensional array

How to create an object from one dimensional array and static string

How do you convert a jQuery object into a string?

How do I convert 1-dimensional array of String to String?