Quickest way to comma separate a Tridion multivalue field
Asked Answered
K

4

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

Kt answered 15/5, 2012 at 12:13 Comment(2)
Quickest way in terms of execution time, or in terms of smallest amount of codeDriest
Thanks for the update @Hendrik - try the link I added in my answer (#799946) The accepted answer deals with converting the IList to an array before performing the join.Paredes
D
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))
Driest answered 15/5, 2012 at 12:29 Comment(1)
This method is the shortest and seems to be quicker as well. Thanks!Kt
P
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)

Paredes answered 15/5, 2012 at 12:34 Comment(2)
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.Kt
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.Paredes
D
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))
Driest answered 15/5, 2012 at 12:29 Comment(1)
This method is the shortest and seems to be quicker as well. Thanks!Kt
S
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)@@
Shipley answered 15/5, 2012 at 14:2 Comment(2)
That's good to remember for Dreamweaver Templating! Thanks DominicKt
You can also evaluate expressions in an assembly TBB.Shipley
S
0

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

Surbased answered 15/5, 2012 at 12:28 Comment(2)
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: #799946Kt
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.Surbased

© 2022 - 2024 — McMap. All rights reserved.