-2
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
Rocket Nikita
  • 470
  • 2
  • 7
  • 20
Sund'er
  • 666
  • 1
  • 4
  • 11
  • This question might help you. It is a bit different, but should be similar other than some string formatting: https://stackoverflow.com/questions/728319/linq-orderby-against-specific-values – bisen2 Nov 28 '20 at 16:17
  • 1
    This sounds like homework. You sould at least shown an attempt. – JHBonarius Nov 28 '20 at 16:18

2 Answers2

2

You can combine String.Join with Select

string result = string.Join(",", fList.Select(n => columnList[n-1]));

Obviously you need to ensure that the index exists. If you dont know that you could use ElementAtOrDefault(n-1) instead of [n-1].

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
-1

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
Igor Pashchuk
  • 2,455
  • 2
  • 22
  • 29