Can LINQ be asked to Skip/Leave particular indexes?
.net, c#, linq, loops
Solution
You can test the index inside `Select` and choose your action accordingly:
NMD = NMD.Select((x, i) => i % 2 == 1 && i < NMD.Length - 1 ? x / 10 : x).ToArray();
// => { 3, 0.5, 6, 6.5, 34, 0.3, 5, 0.6, 65, 3.4, 3, 0.5, 6, 6.5, 34 }
However, as you maybe already figured out by looking at the size of this statement, LINQ is not a conceptual improvement here, because you can only create new sequences with it and not mutate existing sequences.
That said, the `for` loop is fine and actually more readable, in my opinion. Rather than saying that "LINQ is loops++", you should refine it to "LINQ is sequence generation++" or "read-only iteration++".
If you want to use LINQ efficiently, you have to rethink and redesign your code in a more functional way (and use immutable data structures, for example) instead of just replacing every `for` loop with a LINQ expression. If you do that consequently and sensibly, you can increase the quality of your code and make the switch parallel execution less problematic in the future.
Problem
I myself am a big fan of LINQ. The art of brevity is reflected in LINQ. Also in .Net we say that LINQ is a step ahead of loops or we can say LINQ is loops++. But is it really this way? Why i am being judgmental is because i was trying to convert this for loop code to LINQ but was confused that do LINQ skip/Leave indexes? ``` double[] NMD = {3.0, 5.0, 6.0, 65.0, 34.0, 3.0, 5.0, 6.0, 65.0, 34.0, 3.0, 5.0, 6.0, 65.0, 34.0 }; for(int i=1; i<NMD.Length-1; i+=2) NMD[i] = NMD[i]/10; ``` Here i am asking for loop to start from index 1 and stop at the penultimate value and also skip value by 2. So can we do this in LINQ. IMO I don't think so But I'll be happy to be proven wrong.