Linq Map! or Collect!
Asked Answered
I

2

25

What is the Linq equivalent to the map! or collect! method in Ruby?

   a = [ "a", "b", "c", "d" ]
   a.collect! {|x| x + "!" }
   a             #=>  [ "a!", "b!", "c!", "d!" ]

I could do this by iterating over the collection with a foreach, but I was wondering if there was a more elegant Linq solution.

Icing answered 31/3, 2009 at 17:23 Comment(1)
Linq takes a functional approach so you usually won't be doing an in-place modification like in your example above. However, this more matches the expected use of map and collect in ruby (without the !)Franco
N
36

Map = Select

var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!");
Nanny answered 31/3, 2009 at 17:25 Comment(0)
M
23

The higher-order function map is best represented in Enumerable.Select which is an extension method in System.Linq.

In case you are curious the other higher-order functions break out like this:

reduce -> Enumerable.Aggregate
filter -> Enumerable.Where

Mongeau answered 31/3, 2009 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.