Converting Array of Guids to a List

March 21st, 2012 by admin Leave a reply »

I have been doing more web work these days and on a recent project I was working on I had a need to convert an incoming string of guids to a List<Guid> for usage over in our repositories.  Well, my first thought to get the string to be split up into an Array[] of strings was to just call the Split() function in .net, and that will do the job if all you are wanting is an Array[] of strings.  it would look something like this:

The problem was when I tried to get .net to cast that over to a List using Cast or ToList(), it won’t convert this although you would think it would be smart enough to convert an array of strings that are Guids to a List that is Guids, but whatever.  This is where the ConvertAll() method on the Array class comes into play, it is in the .net 4 libarary so make sure you are on the right version.  The ConvertAll() will convert an array of one type to an array of another type, so I have updated my code to look like so:

Ids = Array.ConvertAll(request.Headers[“ids”].Split(‘,’).ToArray(), Guid.Parse).ToList();

Hope this helps somebody :).


Leave a Reply