[c#] C#/Linq: Apply a mapping function to each element in an IEnumerable?

I've been looking for a way to transform each element of an IEnumerable into something else using a mapping function (in a Linq-compatible way) but I haven't found anything.

For a (very simple) example, it should be able to do something like

IEnumerable<int> integers = new List<int>() { 1, 2, 3, 4, 5 };
IEnumerable<string> strings = integers.Transform(i => i.ToString());

But I haven't found anything. I mean, it's pretty straightforward to write an extension method that accomplishes that (basically, all it requires is wrapping the source Enumerator into a new class and then writing a bit of boilerplate code for delegating the calls to it), but I would have expected this to be a fairly elementary operation, and writing it myself feels like reinventing the wheel - I can't shake the feeling that there may be a built-in way which I should be using, and I've just been too blind to see it.

So... is there something in Linq that allows me to do what I described above?

This question is related to c# linq

The answer is


You can just use the Select() extension method:

IEnumerable<int> integers = new List<int>() { 1, 2, 3, 4, 5 };
IEnumerable<string> strings = integers.Select(i => i.ToString());

Or in LINQ syntax:

IEnumerable<int> integers = new List<int>() { 1, 2, 3, 4, 5 };

var strings = from i in integers
              select i.ToString();

You're looking for Select which can be used to transform\project the input sequence:

IEnumerable<string> strings = integers.Select(i => i.ToString());