1

I have a lot of strings in my string array. I want to reverse it via LINQ. Although I can do it using a for loop using Array.Reverse, I wonder if I can do it using LINQ?

This is my code but its not working as expected:

string[] strTemp = new string[] {"Hello", "World", "Foo"};
string[] strResult = strTemp.Select((a, b) => new { Value = a, Index = b })
                   .Select(y => y.Value.ToCharArray().Reverse().ToString()).ToArray();

My expected result should be:

"olleH", "dlroW", "ooF"

2 Answers 2

3
string[] strResult = strTemp.Select(y => string.Concat(y.Reverse())).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like you're trying to reverse the individual strings in the array, and not reverse the order of the elements, is that correct? If so, try this.

string[] strResult = strTemp.Select(s => Array.Reverse(s.ToCharArray()).ToString()).ToArray();

I don't have access to my c# machine, so I can't test this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.