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












2















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.










share|improve this question




















  • 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
















2















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.










share|improve this question




















  • 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














2












2








2








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.










share|improve this question
















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.







c# arrays .net linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












3 Answers
3






active

oldest

votes


















1














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()





share|improve this answer
























  • 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





















1














source.Select(x => new string{
x.Stamp, x.Latency0, x.Latency1,
x.Latency2, x.Latency3, x.Latency4})
.ToArray();





share|improve this answer































    1














    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();





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      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()





      share|improve this answer
























      • 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


















      1














      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()





      share|improve this answer
























      • 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
















      1












      1








      1







      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()





      share|improve this answer













      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()






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 23 '18 at 9:26









      KonamimanKonamiman

      42.7k1598127




      42.7k1598127













      • 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



















      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















      1














      source.Select(x => new string{
      x.Stamp, x.Latency0, x.Latency1,
      x.Latency2, x.Latency3, x.Latency4})
      .ToArray();





      share|improve this answer




























        1














        source.Select(x => new string{
        x.Stamp, x.Latency0, x.Latency1,
        x.Latency2, x.Latency3, x.Latency4})
        .ToArray();





        share|improve this answer


























          1












          1








          1







          source.Select(x => new string{
          x.Stamp, x.Latency0, x.Latency1,
          x.Latency2, x.Latency3, x.Latency4})
          .ToArray();





          share|improve this answer













          source.Select(x => new string{
          x.Stamp, x.Latency0, x.Latency1,
          x.Latency2, x.Latency3, x.Latency4})
          .ToArray();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 9:10









          AomineAomine

          42k74172




          42k74172























              1














              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();





              share|improve this answer




























                1














                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();





                share|improve this answer


























                  1












                  1








                  1







                  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();





                  share|improve this answer













                  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();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 9:22









                  IvvanIvvan

                  405512




                  405512






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      Refactoring coordinates for Minecraft Pi buildings written in Python