Just one curious question from me, is there any transformation function in linq. I mean if i have List<int> or List<Foo>, i would like to change the elements at say index x or whoever satisfy the condition in Where.
Just one curious question from me, is there any transformation function in linq. I
Share
If you’re looking to conditionally project a Foo to another Foo (leaving others untouched), you can do something like:
On the other hand, if you only want to project Foos that match the condition:
Do note that both of these return new sequences – LINQ isn’t normally used to modify existing collections. You could of course materialize the results into a collection, overwriting an existing variable if necessary.
Since you specifically mention lists, there is another non-LINQ immediate-execution alternative to the first query – the
List<T>.ConvertAllmethod:EDIT: Sounds like you’re looking for a “ReplaceWhere” method – as far as I know, there is no direct framework method that replaces the elements of a list based on a predicate. It’s easy to write one yourself though:
Usage: