Warm tip: This article is reproduced from serverfault.com, please click

How to filter String array with integer array with LINQ by index?

发布于 2020-11-28 16:09:10
String[] columnList = new String[]{"column1","column2","column3","column4","column5","column6","column7","column8","column9","column10","column11","column12" };

Int[] fList = new Int[] { 1,3,5,10,12 };

I want output like this:

column1,column3,column5,column10,column12
Questioner
Sunder Sakthivel
Viewed
0
Igor Pashchuk 2020-11-29 00:26:34

I hope this is not homework! This will do the trick:

var joined = from column in columnList.Select((x, i) => new {Index = i + 1, Column = x} )
             join index in fList on column.Index equals index
             select column.Column;
Console.WriteLine(string.Join(",", joined));

// output
// column1,column3,column5,column10,column12