How to convert Guid[] to String?
Asked Answered
P

7

10

Something like String.Join(",", new string[] { "a", "b" });, but for Guid[]

var guids = new Guid[] { Guid.Empty, Guid.Empty };

var str = /* Magic */

// str = 00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000
Palomo answered 26/7, 2010 at 15:10 Comment(0)
P
25
var str = guids.Select(g => g.ToString())
               .Aggregate((working, next) => working + "," + next);

Once your list of Guids starts growing, this method of concatenation is going to cause performance issues. You can modify it to use a StringBuilder:

var str = guids.Select(g => g.ToString())
               .Aggregate(new StringBuilder(),
                          (sb, str) => sb.Append("," + str),
                          sb => sb.ToString());

Both of those are the complicated LINQ Extension method way of doing things. You could also simply use String.Join:

var str = String.Join(",", guids.Select(g => g.ToString()).ToArray());
Petcock answered 26/7, 2010 at 15:13 Comment(5)
I'd be curious at how fast it works compared to other methods. I'm guessing Aggregate doesn't use String.Concat to allocate memory correctly.Northwesterly
@Palomo - Just keep in mind that with large lists of Guids, you might run into perf issues (a lot of string concatenation). In that case, you might need to switch to the StringBuilder method (and manually remove the last comma).Petcock
I will, but SO has a delay. Just wait 3 minutes :PPalomo
@Yuriy Faktorovich - Aggregate will use whatever concatenation method you specify.Petcock
@Justin Niessner: Right, but if you only pass two arguments to String.Concat it won't have any improvement over the + operator. The + operator I believe, don't quote me on this, already calls the String.Concat.Northwesterly
A
22

.NET 4 added a String.Join<T>(string separator, IEnumerable<T> values method. So, in .NET 4, just this will work:

String.Join(",", guids);
Awn answered 26/7, 2010 at 15:31 Comment(0)
N
20
String.Join(",", guids.Select(g => g.ToString()).ToArray());
Northwesterly answered 26/7, 2010 at 15:14 Comment(1)
.NET 4 added a generic Join method which you can just pass the guids array to without modificationsAwn
G
5

I thought this would work?

StringBuilder stringBuilder = new StringBuilder();
int i = 0;
foreach (var guid in guids)
{
    stringBuilder.Append(guid.ToString());
    if (++i < guids.Length)
    { 
        stringBuilder.Append(","); 
    }
}

var str = stringBuilder.ToString();
Government answered 26/7, 2010 at 15:12 Comment(4)
You forgot the , between GUIDs ;)Cosmogony
That will put an extra comma at the end.Northwesterly
Ok, I tried to fix the comma issue quickly. It's untested. I hope it works :-)Government
as a suggestion, I would append the comma before appending the guid, but only if the stringbuilder.length > 0Wivinah
D
1

if your framework >= .NET 3.5


String.Join(",", (from g in guids select g.ToString()).ToArray())
Derman answered 26/7, 2010 at 15:15 Comment(0)
P
1
String.Join(",", Array.ConvertAll(guids, g => g.ToString()));
Parlay answered 26/7, 2010 at 15:33 Comment(2)
It seems this statement can't pass compilation in VS2010?Unguent
Array.ConvertAll needs a class that implements Converter<TInput, TOutput>, you're passing in a method.Northwesterly
F
0
//Convert string to List<string>    
List<string> guidList = your_string.Split(',').ToList<string>();

//Convert string to List<Guid>    
List<Guid> guidList = your_string.Split(',').ToList<string>().ConvertAll<Guid>(g => new Guid(g));
Ferree answered 29/6, 2016 at 12:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.