Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate a constant string to each item in a List<string> using LINQ

I am using C# and .Net 4.0.

I have a List<string> with some values, say x1, x2, x3. To each of the value in the List<string>, I need to concatenate a constant value, say "y" and get back the List<string> as x1y, x2y and x3y.

Is there a Linq way to do this?

like image 377
itsbalur Avatar asked Oct 22 '12 07:10

itsbalur


2 Answers

List<string> yourList = new List<string>() { "X1", "Y1", "X2", "Y2" };
yourList = yourList.Select(r => string.Concat(r, 'y')).ToList();
like image 199
Habib Avatar answered Nov 14 '22 12:11

Habib


list = list.Select(s => s + "y").ToList();
like image 7
Sergey Berezovskiy Avatar answered Nov 14 '22 11:11

Sergey Berezovskiy