Question: I have this for loop that searches a list for specific values and replaces them with new ones. 

```
for (int i = 0; i < Data.Count; i++) {

if (Data[i] > 0 && Data[i] <= 10) {Data[i] = 1;}
else if (Data[i] > 10 && Data[i] < 20) {Data[i] = 2;}      
...
}

```

I've been trying to write this function in linq and I know it can be written this way:

```
var Data2 = Data.Where(x=> x > 0 && x <= 10).Select(y=> y=1).ToList();

```

My question is that is there any way to convert this for loop into linq form without the need to declare new lists? I mean a linq form which searches for these values inside the list and when it finds them it replaces them accordingly.

Answer: 
The exact equivalent would be:

```
Data
  .Select((e,i) => new { Element = e, Index = i })
  .Where(ei => ei.Element > 0 && ei.Element < 20)
  .ToList()
  .ForEach(ei => Data[ei.Index] = (ei.Element <= 10) ? 1 : 2);

```

or second possibility: look at [Save](https://stackoverflow.com/questions/18427812/what-would-be-the-equivalent-this-for-loop-in-linq/18427840#18427840)'s answer.

However it's still creating a List in-between (`ForEach` comes only for `List<T>`). It's not very readable, better to use non-mutable approach and just generate new list basing on given criteria.
Modifying data in-place is not Linq's strength. Linq methods generally are side-effects free. That is why the .ForEach() method is not a Linq method and is only defined for lists.

However there is nothing preventing you from making Linq functions with side-effects.
So I would not recommend this solution, but you can do anything within a select statement, even modifying the underlying list. There is also a select overload that uses the element and its index as parameters ( Select( (element,index) => ... ). 

So you can do anything with select you can do in a for loop. But I would recommend the for loop for readability.

```
data.Select((d,i)=>
    {if (d > 0 && d <= 10) data[i]=1 else if (d>10 && d<20 data[i]=2;})
    .All(d=>true); // <-- note that you do need some way to consume the IEnumerable
                   // in order to execute the .Select(). You can use ToList(), Count(), ..

```