如何使用linq从索引数组转到对象集合?

Rya*_*ker 2 c# linq arrays collections

我的标题问题有点模糊,因为很难问,但我的情况是这样的:

我有一个int数组,它们是一个单独的对象集合的索引.

该数组如下所示:

int[] indices = { 0, 2, 4, 9, 10, 11, 13, /* more (non-)sequential indices */ };
Run Code Online (Sandbox Code Playgroud)

这些索引中的每一个都对应于我拥有的集合中该索引处的对象.

我希望能够使用我的数组中的索引构建这些对象的新集合.

我如何使用一些LINQ函数?

小智 5

int[] indices = { 0, 2, 4, 9, 10, 11, 13 };
string[] strings = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q" };

IEnumerable<string> results = indices.Select(s => strings[s]);

// or List<string> results = indices.Select(s => strings[s]).ToList();

foreach (string result in results) // display results
{
    Console.WriteLine(result);
}
Run Code Online (Sandbox Code Playgroud)

当然,将字符串等更改为您的对象集合.