获取索引为int []索引的string []元素

ssh*_*how 4 .net c# linq lambda

我有一个string[],并希望获得string[]具有索引的元素,其中我知道存在,在一个中指定int[].

string[] stringArray = { "a", "b", "c", "d", "e", "f", "g" };
int[] indices = { 1, 2, 4, 6 };
Run Code Online (Sandbox Code Playgroud)

由此,我试图得到一个string[]包含{ "b", "c", "e", "g" }.优选使用λ表达.我该怎么做?

rec*_*ive 10

你能做到的一种方法就是这样.

string[] result = indices.Select(i => stringArray[i]).ToArray()
Run Code Online (Sandbox Code Playgroud)


Ken*_*rey 6

indices.Select(i => stringArray[i]);
Run Code Online (Sandbox Code Playgroud)

  • @SaeedAmiri我对我的问题进行了编辑,以强调我知道指数存在. (2认同)