LINQ is just for queries because it stands for Language Integrated Query. But please don’t think of it only in that context. Normally we use to write a loop to iterate through the array of strings and populate a newly constructed array of integers. But LINQ makes much easier by writing a single line code for the same.
The following example helps us to convert an array of string to integer array with sorted result.
. Declare an array of strings
string[] numbers = { "0042", "010", "9", "27" };
.Convert the array of strings to an array of integer
int[] nums = numbers.Select(s => Int32.Parse(s)).ToArray();
.Sorting the converted array of interger
int[] nums = numbers.Select(s => Int32.Parse(s)).OrderBy(s => s).ToArray();
. To display the resulting array of integers
foreach(int num in nums)
Console.WriteLine(num);
OUTPUT :
9
10
27
42
Share your thoughts with me !!!
- Rangoli
The following example helps us to convert an array of string to integer array with sorted result.
. Declare an array of strings
string[] numbers = { "0042", "010", "9", "27" };
.Convert the array of strings to an array of integer
int[] nums = numbers.Select(s => Int32.Parse(s)).ToArray();
.Sorting the converted array of interger
int[] nums = numbers.Select(s => Int32.Parse(s)).OrderBy(s => s).ToArray();
. To display the resulting array of integers
foreach(int num in nums)
Console.WriteLine(num);
OUTPUT :
9
10
27
42
Share your thoughts with me !!!
- Rangoli