Freigeben über


How to Convert a list of Object/String to an Array Using LINQ?

This is something that I really like, I can convert a list of objects into an array of something.

I had a need to convert a list of string into an array of string, of course it can be done easily in several different ways, creating an array of int, converting the string element, and populate the int array, or using List<int> as container.

Using LINQ, is very simple.

 string[] Origin = new string[] { "1", "2", "3", "4", "5" };
int[] Result = Origin.Select(item => Convert.ToInt32(item)).ToArray();

Less code to write. :)

Comments

  • Anonymous
    March 18, 2008
    The comment has been removed