Reputation: 371
I have following codes (I am a beginner);
string myVar = "abcd";
var myQuery = from x in myVar select x;
MessageBox.Show( string.Join("\n", myQuery));
I'd like to have the result like this;
1 a
2 b
3 c
4 d
Some hints please.
Thanks.
Upvotes: 2
Views: 69
Reputation: 236318
You should use Select
operator overload which accepts index of item. (unfortunately it's not available in query syntax):
String.Concat(myVar.Select((ch,i) => $"{i + 1} {ch}\n"))
Update: @Mattew correctly noticed that this code will add extra \n
at the end. If it's not good, then you should use String.Join
instead. Also consider to use Environment.NewLine
thus for windows platform new line string is \r\n
:
String.Join(Environment.NewLine, myVar.Select((ch,i) => $"{i + 1} {ch}"))
Upvotes: 8