Combining a constant string into each item in a List <string> using LINQ
I am using C # and .Net 4.0.
I have a List<string> with some values, for example x1, x2, x3. For each value in the List<string> I need to associate a constant value, such as "y", and return the List<string> as x1y, x2y and x3y.
Is there a Linq way for this?
+8
itsbalur
source share4 answers
List<string> yourList = new List<string>() { "X1", "Y1", "X2", "Y2" }; yourList = yourList.Select(r => string.Concat(r, 'y')).ToList(); +9
Habib
source share list = list.Select(s => s + "y").ToList(); +4
Sergey Berezovskiy
source shareAlternative using ConvertAll :
List<string> l = new List<string>(new [] {"x1", "x2", "x3"} ); List<string> l2 = l.ConvertAll(x => x + "y"); +3
Paolo tedesco
source shareYou can use Select for this
var list = new List<string>(){ "x1", "x2" }; list = list.Select(s => s + "y").ToList(); +1
Asif mushtaq
source share