12

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

Hendrik Beenker
  • 1,120
  • 5
  • 17
  • Quickest way in terms of execution time, or in terms of smallest amount of code – Andrey Marchuk May 15 '12 at 12:42
  • Thanks for the update @Hendrik - try the link I added in my answer (http://stackoverflow.com/questions/799446/creating-a-comma-separated-list-from-iliststring-or-ienumerablestring) The accepted answer deals with converting the IList to an array before performing the join. – Mike Percival May 15 '12 at 13:28

4 Answers4

9

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)

Community
  • 1
  • 1
Mike Percival
  • 595
  • 3
  • 12
  • Unfortunately in 3.5 the nice and clean .NET 4 method is not yet available. I checked out the other links, but the solutions there won't make the script much smaller. The answer by user978511 is short and quick. – Hendrik Beenker May 15 '12 at 13:30
  • 1
    Fair enough - glad it's working for you. I find for simple things LINQ can be quite difficult to read and support compared to the slightly longer approach but if there are performance improvements, then I'd agree with your accepted answer. – Mike Percival May 15 '12 at 13:33
6

You can user LINQ:

var multiTextField = fields["multiTextField"] as TextField;
string delimeter = ",";     
Console.WriteLine(multiTextField.Values.Aggregate((i, j) => i + delimeter + j))

Or in shorter (uglier) way:

((TextField) fields["multiTextField"]).Values.Aggregate((i, j) => i + "," + j))
Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
4

Evaluate the expression. A multi-valued text field is already a comma-separated string "under the water", so you could just grab it in your Dreamweaver layer like this:

@@(Component.multiValueField)@@
Dominic Cronin
  • 6,062
  • 2
  • 23
  • 56
0

Have you tried multiTextField.Values.ToArray().Join(",")?

Peter Kjaer
  • 4,316
  • 13
  • 23
  • I am sorry @peter-kjaer, the ToArray method is a LINQ method you have to create first. At least that's what it seems like in this link: http://stackoverflow.com/questions/799446/creating-a-comma-separated-list-from-iliststring-or-ienumerablestring – Hendrik Beenker May 15 '12 at 13:32
  • I saw it in the official documentation, but I didn't have time to actually try it out. My first thought was "Join" so I just went through the properties in the documentation and posted a suggestion. – Peter Kjaer May 15 '12 at 13:43