我正在使用 C# 和 .Net 4.0。
我有List<string>
一些值,比如 x1、x2、x3。对于 中的每个值List<string>
,我需要连接一个常量值,比如“y”并取回List<string>
x1y、x2y 和 x3y。
有没有一种 Linq 方法可以做到这一点?
我正在使用 C# 和 .Net 4.0。
我有List<string>
一些值,比如 x1、x2、x3。对于 中的每个值List<string>
,我需要连接一个常量值,比如“y”并取回List<string>
x1y、x2y 和 x3y。
有没有一种 Linq 方法可以做到这一点?
List<string> yourList = new List<string>() { "X1", "Y1", "X2", "Y2" };
yourList = yourList.Select(r => string.Concat(r, 'y')).ToList();
list = list.Select(s => s + "y").ToList();
另一种方法,使用ConvertAll
:
List<string> l = new List<string>(new [] {"x1", "x2", "x3"} );
List<string> l2 = l.ConvertAll(x => x + "y");
你可以使用Select
它
var list = new List<string>(){ "x1", "x2" };
list = list.Select(s => s + "y").ToList();