0

Theres a Do method in System.Reactive to a execute a mutator for each item in the sequence.

Is there any equivalent method for IEnumerable either in standard library or third parties like morelinq?

SuperJMN
  • 13,110
  • 16
  • 86
  • 185

1 Answers1

1

LINQ query operators are generally meant to be free from side-effects. You could just use a foreach loop to "do" your thing.

Or do it in a Select or Where method if you don't care about side effects:

enumerable.Select(x => { /*do something here */ return x; })

For a List<T>, there is a ForEach extension method that can be used to execute an Action<T> for each element.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • 1
    The Do method executes the action as parte of the pipeline. ForEach does this for a materialized list, that isn't what I need. I need a pull-version of it. – SuperJMN Oct 23 '20 at 14:28
  • I know, hence the "For a `List`..." in the answer. So use any of my first two suggestions? – mm8 Oct 23 '20 at 14:29