Can we replace a for loop iteration with LinQ?

FranK Duc 121 Reputation points
2021-06-30T13:12:16.907+00:00

Hello,

I need to do this:

var listm = new List<double>();


             for(int z = 0; z < 40; z++)
                  {


                fibo2 = (listm[z] - lowPrice0) / (Ncma - lowPrice0);

// Is there a way to replace the for loop with some Linq method to do the same?

//I am getting lags above iteration 100 and crash above 150. The for loop is part of a nested for loop.
//Maybe it wont solve the problem but i can at least test something else.

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2021-06-30T14:47:00.6+00:00

    The code you've provided is unusable as it contains unknown variables and most likely the "crash" is caused by logic within the loop not the loop itself.

        class Program
        {
            private static readonly Random _random = new Random();
    
            static void Main(string[] args)
            {
                double lowPrice = 2.2d;
                double Ncma = 8;
    
    
                List<double> items = PopulateList(10);
    
                items.ForEach(i => {
                    double value = (i - lowPrice) / (Ncma - lowPrice);
                    Console.WriteLine(value);
                });
            }
    
            public static List<double> PopulateList(int iterations)
            {
                List<double> items = new List<double>();
                for (int i = 0; i< iterations; i++)
                {
                    items.Add(i + _random.Next(100));
                }
    
                return items;
            }
    
        }
    
    0 comments No comments