tags:

views:

1122

answers:

2

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.

+16  A: 

Map = Select

var x = new string[] { "a", "b", "c", "d"}.Select(s => s+"!");
Quintin Robinson
+6  A: 

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

Andrew Hare