9

我正在使用 C# 和 .Net 4.0。

我有List<string>一些值,比如 x1、x2、x3。对于 中的每个值List<string>,我需要连接一个常量值,比如“y”并取回List<string>x1y、x2y 和 x3y。

有没有一种 Linq 方法可以做到这一点?

4

4 回答 4

15
List<string> yourList = new List<string>() { "X1", "Y1", "X2", "Y2" };
yourList = yourList.Select(r => string.Concat(r, 'y')).ToList();
于 2012-10-22T07:06:53.913 回答
7
list = list.Select(s => s + "y").ToList();
于 2012-10-22T07:04:51.747 回答
5

另一种方法,使用ConvertAll

List<string> l = new List<string>(new [] {"x1", "x2", "x3"} );
List<string> l2 = l.ConvertAll(x => x + "y");
于 2012-10-22T07:08:38.440 回答
1

你可以使用Select

var list = new List<string>(){ "x1", "x2" };

list = list.Select(s =>  s + "y").ToList();
于 2012-10-22T07:04:44.380 回答