How do you convert a List of object to a one dimensional array?
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.
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.
I tried using this ToArray()
on the list but it only converts it into an array of object.
c# arrays .net linq
add a comment |
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.
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.
I tried using this ToArray()
on the list but it only converts it into an array of object.
c# arrays .net linq
1
Possible duplicate of Customizing Json.NET serialization: turning object into array to avoid repetition of property names
– Access Denied
Nov 23 '18 at 9:09
add a comment |
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.
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.
I tried using this ToArray()
on the list but it only converts it into an array of object.
c# arrays .net linq
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.
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.
I tried using this ToArray()
on the list but it only converts it into an array of object.
c# arrays .net linq
c# arrays .net linq
edited Nov 23 '18 at 11:26
Daniel Marín
421323
421323
asked Nov 23 '18 at 9:06
ankurankur
2,000124575
2,000124575
1
Possible duplicate of Customizing Json.NET serialization: turning object into array to avoid repetition of property names
– Access Denied
Nov 23 '18 at 9:09
add a comment |
1
Possible duplicate of Customizing Json.NET serialization: turning object into array to avoid repetition of property names
– Access Denied
Nov 23 '18 at 9:09
1
1
Possible duplicate of Customizing Json.NET serialization: turning object into array to avoid repetition of property names
– Access Denied
Nov 23 '18 at 9:09
Possible duplicate of Customizing Json.NET serialization: turning object into array to avoid repetition of property names
– Access Denied
Nov 23 '18 at 9:09
add a comment |
3 Answers
3
active
oldest
votes
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()
I think OP's json sample shows thatLatency
values should be a stringlatency1: "120.02"
, if you serialize decimal value output will belatency1: 120.02
– Fabio
Nov 23 '18 at 9:44
add a comment |
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1,
x.Latency2, x.Latency3, x.Latency4})
.ToArray();
add a comment |
Aomine is right, but if you want to get the result as array of doubles (or actually nullable doubles), you need to do convertion like this:
double temp;
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1, x.Latency2, x.Latency3, x.Latency4}
.Select(n => double.TryParse(n, out temp) ? temp : (double?)null))
.ToArray();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443542%2fhow-do-you-convert-a-list-of-object-to-a-one-dimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()
I think OP's json sample shows thatLatency
values should be a stringlatency1: "120.02"
, if you serialize decimal value output will belatency1: 120.02
– Fabio
Nov 23 '18 at 9:44
add a comment |
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()
I think OP's json sample shows thatLatency
values should be a stringlatency1: "120.02"
, if you serialize decimal value output will belatency1: 120.02
– Fabio
Nov 23 '18 at 9:44
add a comment |
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()
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()
answered Nov 23 '18 at 9:26
KonamimanKonamiman
42.7k1598127
42.7k1598127
I think OP's json sample shows thatLatency
values should be a stringlatency1: "120.02"
, if you serialize decimal value output will belatency1: 120.02
– Fabio
Nov 23 '18 at 9:44
add a comment |
I think OP's json sample shows thatLatency
values should be a stringlatency1: "120.02"
, if you serialize decimal value output will belatency1: 120.02
– Fabio
Nov 23 '18 at 9:44
I think OP's json sample shows that
Latency
values should be a string latency1: "120.02"
, if you serialize decimal value output will be latency1: 120.02
– Fabio
Nov 23 '18 at 9:44
I think OP's json sample shows that
Latency
values should be a string latency1: "120.02"
, if you serialize decimal value output will be latency1: 120.02
– Fabio
Nov 23 '18 at 9:44
add a comment |
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1,
x.Latency2, x.Latency3, x.Latency4})
.ToArray();
add a comment |
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1,
x.Latency2, x.Latency3, x.Latency4})
.ToArray();
add a comment |
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1,
x.Latency2, x.Latency3, x.Latency4})
.ToArray();
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1,
x.Latency2, x.Latency3, x.Latency4})
.ToArray();
answered Nov 23 '18 at 9:10
AomineAomine
42k74172
42k74172
add a comment |
add a comment |
Aomine is right, but if you want to get the result as array of doubles (or actually nullable doubles), you need to do convertion like this:
double temp;
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1, x.Latency2, x.Latency3, x.Latency4}
.Select(n => double.TryParse(n, out temp) ? temp : (double?)null))
.ToArray();
add a comment |
Aomine is right, but if you want to get the result as array of doubles (or actually nullable doubles), you need to do convertion like this:
double temp;
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1, x.Latency2, x.Latency3, x.Latency4}
.Select(n => double.TryParse(n, out temp) ? temp : (double?)null))
.ToArray();
add a comment |
Aomine is right, but if you want to get the result as array of doubles (or actually nullable doubles), you need to do convertion like this:
double temp;
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1, x.Latency2, x.Latency3, x.Latency4}
.Select(n => double.TryParse(n, out temp) ? temp : (double?)null))
.ToArray();
Aomine is right, but if you want to get the result as array of doubles (or actually nullable doubles), you need to do convertion like this:
double temp;
source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1, x.Latency2, x.Latency3, x.Latency4}
.Select(n => double.TryParse(n, out temp) ? temp : (double?)null))
.ToArray();
answered Nov 23 '18 at 9:22
IvvanIvvan
405512
405512
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443542%2fhow-do-you-convert-a-list-of-object-to-a-one-dimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of Customizing Json.NET serialization: turning object into array to avoid repetition of property names
– Access Denied
Nov 23 '18 at 9:09