-2

I am really new in c# programming. I searched several hours, but can't find a solution or I don't understand how can I solve this problem. I deserialized objects from a dataset . my objects have trading specific values for example open high low close. I want to iterate through my objects and get some values in a float array. the array should look like this float[] rsi14 ={ 7520.5, 7535.0, 7530.5, ...}

Like this:

deserialziedObjectRsi[0].open + desirializedObjectRsi[1].open + ... to array.

These values are already from type float.

I want this in a loop, so I don't have to write this 50 times or more.

Here is some code that I have tried:

    var deserializedObjectRsi = 
        JsonConvert.DeserializeObject<List<HistoricPricesRsi>>(stochDataRSI);

       List<float> RsiClose = new List<float>();
            RsiClose.Add(deserializedObjectRsi[0].close);
            RsiClose.Add(deserializedObjectRsi[1].close);
            RsiClose.Add(deserializedObjectRsi[2].close);
            RsiClose.Add(deserializedObjectRsi[3].close);
            RsiClose.Add(deserializedObjectRsi[4].close);
            RsiClose.Add(deserializedObjectRsi[5].close);
            RsiClose.Add(deserializedObjectRsi[6].close);
            RsiClose.Add(deserializedObjectRsi[7].close);
            RsiClose.Add(deserializedObjectRsi[8].close);
            RsiClose.Add(deserializedObjectRsi[9].close);
            RsiClose.Add(deserializedObjectRsi[10].close);
            RsiClose.Add(deserializedObjectRsi[11].close);
            RsiClose.Add(deserializedObjectRsi[12].close);
            RsiClose.Add(deserializedObjectRsi[13].close);

            float[] arrRSI14 = RsiClose.ToArray();
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • 3
    Welcome to Stack Overflow. What does "deserialziedobject.open [0-13] to array" mean? Please try to describe in more detail what your goal is. Repeating yourself isn't more detail. You need to say it differently. – 15ee8f99-57ff-4f92-890c-b56153 Oct 09 '19 at 17:44
  • Post a [mcve] (on dotnetfiddle for example) with an example of input and output – aloisdg Oct 09 '19 at 17:45
  • I think that what you need is `var RsiClose = deserializedObjectRsi.Select(x => Convert.ToDecimal(x.Close)).ToList();`. It looks like you are using the kind of data where you should use the `decimal` type: [decimal vs double! - Which one should I use and when?](https://stackoverflow.com/q/1165761/1115360) – Andrew Morton Oct 09 '19 at 17:56
  • I updated my question, hope now its a bit clrearer. – Maximilian Sanktjohanser Oct 10 '19 at 08:46

1 Answers1

1

You can use LINQ to process the data for you in a declarative way. There will be an implied loop in it.

var arrRSI14 = deserializedObjectRsi.Select(x => Convert.ToSingle(x.Close)).ToArray();

What that does is, for each value (represented as x, but you can use any name which makes sense) in deserializedObjectRsi, convert it to a single, and put the results into an array. The var keyword makes the compiler figure out what the type is; you could write float[] if you really wanted to.

In C#, the float keyword is an alias of the .NET Framework Single type (please see C# Float vs. VB.net Single - Namin' complainin' for more information), which is why the .NET Convert method shown uses ToSingle instead of ToFloat.


I wish to add a note on the data type used: a Single, or float, is often a poor choice for monetary data as its precision is small enough that noticeable errors accumulate quickly if they are used in calculations. I recommend using the Decimal type when it is important that numbers are not approximated to binary fractions behind the scenes, for example 0.1 in decimal is a recurring binary fraction and cannot be represented exactly in a Single or Double.

For an informative read: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84