question

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 asked DanielZhang-MSFT commented

Can we replace a for loop iteration with LinQ?

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


dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LINQ is simply another way to write a loop. There must be something else wrong with the code if it can only handle 150 iterations.

Is there an error message that goes along with the crash? Can you provide code the community can run that causes the crash?

0 Votes 0 ·

Unfortunately no. Tried to remove some lines and move some script. Displace called variables. Nothing yet.

0 Votes 0 ·

Hi FranKDuc-4126,
Since you cannot provide the code that caused the crash, we cannot determine the specific cause of the crash. As AgaveJoe said, the "crash" is probably caused by the logic within the loop rather than the loop itself.
For your initial question, you can change the for loop you provided to the following linq.

 Enumerable.Range(0, 40)
   .ToList()
   .ForEach(i => fibo2 = (listm[z] - lowPrice0) / (Ncma - lowPrice0));

Best Regards,
Daniel Zhang

0 Votes 0 ·

It wont solve the problem because that method cant be applied in my case. But thanks for noticing it.

0 Votes 0 ·

Hi @FranKDuc-4126,
To solve your actual problem, more relevant code to reproduce the problem is still necessary.
Best Regards,
Daniel Zhang

0 Votes 0 ·

1 Answer

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

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;
         }
    
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.