How to convert array of string to array of integer using LINQ

LINQ (Language integrated query) is a compelling feature of c#. By using LINQ, we can simplify the code very easily in fewer lines of code.
In this example, I will show you how to convert an array of string to the collection of an integer using LINQ select operator

Let’s consider the following array

var inputs=new[]{"1","2","3","10","4"};

Now, you want to convert to integer array using LINQ.
It’s simple to apply for the select operator and pass Convert.ToInt32 as an argument. As shown below

void Main()
{
	var inputs=new[]{"1","2","3","10","4"};
	var intArray=inputs.Select(i => Convert.ToInt32(i));
	Console.WriteLine(intArray);
	Console.WriteLine(intArray.GetType());
}
LINQ Convert to integer

Reactions

Post a Comment

0 Comments

Close Menu