What would be the quickest way to have a multivalue Tridion text field split into a comma separated string? In my case I am using C#, but I guess any other examples are welcome as well. This is the ugly and long way it seems:
var multiTextField = fields["multiTextField"] as TextField;
string multiCommaField = String.Empty;
for (int i = 0; i < multiTextField.Values.Count; i++)
{
multiCommaField += multiTextField.Values[i].ToString() + ",";
}
Edit: I am using .NET 3.5 and Tridion 2009 SP1
You haven't specified your version of Tridion or .Net in your question, but there are a few different techniques you could use to get comma separated values from the textfield.
If you're using .Net 4, I believe you could just do:
string.Join(",", multiTextField.Values);
as long as multiTextField.Values
implements IList.
If you're using .Net 3.5 or earlier, I believe that the string.Join()
function requires an array rather than IList
.
There was a pretty good discussion regarding the options here String Join on a List (.Net 4)
or here Trying to string.Join an IList (.Net 4)
or here Creating a comma separated list from IList (.Net 3.5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With