tags:

views:

244

answers:

6

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
+5  A: 

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();
DaveDev
You forgot the `,` between GUIDs ;)
Oded
That will put an extra comma at the end.
Yuriy Faktorovich
Ok, I tried to fix the comma issue quickly. It's untested. I hope it works :-)
DaveDev
I've modified and tested it.
Yuriy Faktorovich
@DaveDev, as a suggestion, I would append the comma before appending the guid, but only if the stringbuilder.length > 0
Nathan Koop
+10  A: 
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());
Justin Niessner
Wow, perfect! Thanks!
BrunoLM
@Bruno, mark it as the accepted answer if it's what worked for you!! :-)
DaveDev
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.
Yuriy Faktorovich
@BrunoLM - 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).
Justin Niessner
I will, but SO has a delay. Just wait 3 minutes :P
BrunoLM
@Yuriy Faktorovich - Aggregate will use whatever concatenation method you specify.
Justin Niessner
@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.
Yuriy Faktorovich
+15  A: 
String.Join(",", guids.Select(g => g.ToString()).ToArray());
Yuriy Faktorovich
Nathan Koop
.NET 4 added a generic `Join` method which you can just pass the `guids` array to without modifications
thecoop
@thecoop: Noted, thank you.
Yuriy Faktorovich
+1  A: 

if your framework >= .NET 3.5


String.Join(",", (from g in guids select g.ToString()).ToArray())
ULysses
+5  A: 

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

String.Join(",", guids);
thecoop
That's nice to know, thanks.
BrunoLM
+1  A: 
String.Join(",", Array.ConvertAll(guids, g => g.ToString()));
onof
It seems this statement can't pass compilation in VS2010?
Thomson Tan
Array.ConvertAll needs a class that implements `Converter<TInput, TOutput>`, you're passing in a method.
Yuriy Faktorovich