In this article we will see how we can reverse the array using LINQ and we can put certain condition also while reversing. Reversing of array become real easy using LINQ.
Let's see how we can do this.
string[] numbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; var reverseNumbers = ( from n in numbers select n).Reverse();
foreach (var n in reverseNumbers) { Response.Write("Digit is " + n + "<br/>"); }
Above one was simple reversing of array without any condition.
Let's put some condition while reversing the array.
string[] num = { "zero", "one", " ", "two", "three", "four", "five", "six", "seven", "eight", "nine", " " }; var reversedNum = ( from n in num where n.Trim().Length > 0 select n).Reverse();
foreach (var n in reversedNum) { Response.Write("Digit is " + n + "<br/>"); }
In the above example we are reversing the array only if the length of the string is greater than zero.
This ends the article of array reversig using LINQ
|