Search code examples
.net-3.5tridiontridion2009

Quickest way to comma separate a Tridion multivalue field


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


Solution

  • 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))