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
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());
.NET 4 added a String.Join<T>(string separator, IEnumerable<T> values
method. So, in .NET 4, just this will work:
String.Join(",", guids);
String.Join(",", guids.Select(g => g.ToString()).ToArray());
Join
method which you can just pass the guids
array to without modifications –
Awn 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();
,
between GUIDs ;) –
Cosmogony if your framework >= .NET 3.5
String.Join(",", (from g in guids select g.ToString()).ToArray())
String.Join(",", Array.ConvertAll(guids, g => g.ToString()));
Converter<TInput, TOutput>
, you're passing in a method. –
Northwesterly //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));
© 2022 - 2024 — McMap. All rights reserved.