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
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
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();
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());
String.Join(",", guids.Select(g => g.ToString()).ToArray());
if your framework >= .NET 3.5
String.Join(",", (from g in guids select g.ToString()).ToArray())
.NET 4 added a String.Join<T>(string separator, IEnumerable<T> values
method. So, in .NET 4, just this will work:
String.Join(",", guids);