Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest Conversion of Array of Guid to String and Back?

Tags:

arrays

guid

linq

With .NET 4.0, I sometimes have a Guid[] or 200 items that all need conversion to string[], and sometimes I have to do the reverse.

What is the fastest/smartest way to do that?

Thanks.

like image 245
Snowy Avatar asked Apr 18 '11 01:04

Snowy


3 Answers

An alternative to LINQ is Array.ConvertAll() in this case:

Guid[] guidArray = new Guid[100];
...
string[] stringArray = Array.ConvertAll(guidArray, x => x.ToString());
guidArray = Array.ConvertAll(stringArray, x => Guid.Parse(x));

Runtime performance is probably the same as LINQ, although it is probably a tiny bit faster since it's going from and to array directly instead of an enumeration first.

like image 105
BrokenGlass Avatar answered Sep 20 '22 09:09

BrokenGlass


Well, if you wanted to use LINQ, myList.Select(o => o.ToString()) will do it.

Otherwise, filling a List with a foreach loop is almost as succinct, and will work with older versions of .Net.

like image 39
BlueRaja - Danny Pflughoeft Avatar answered Sep 22 '22 09:09

BlueRaja - Danny Pflughoeft


See if it helps:

Guid[] guids = new Guid[200];
var guidStrings = guids.Select(g => g.ToString());
var revertedGuids = guidStrings.Select(g => Guid.Parse(g));
like image 33
Howard Avatar answered Sep 24 '22 09:09

Howard