C# Convert Collection to Delimited String using Linq

Several times I have had the need to take a collection and convert it to a delimited string for displaying in the UI.  I have been using the code below that I found on a blog that I have since lost the link to. However, while at the SEVDNUG meeting tonight Rob showed me an easier way to get the same output using LINQ.

Original Code:

public class CollectionUtilities 
{ 
    public string Join<T>(string delimiter, IEnumerable<T> items, Converter<T, string> converter) 
    { 
        StringBuilder builder = new StringBuilder(); 
        foreach (T item in items) 
        { 
            string converted = converter(item); 
            if (string.IsNullOrEmpty(converted) == false) 
            { 
                builder.Append(converted); 
                builder.Append(delimiter); 
            } 
        } 
        if (builder.Length > 0) 
        {
            builder.Length = builder.Length - delimiter.Length; 
        }

        return builder.ToString(); 
    } 
}
 

New Code:

public class CollectionUtilities 
{ 
    public string Join<T>(string delimiter, IEnumerable<T> items, Converter<T, string> converter) 
    { 
          return string.Join(delimiter, (items.Where(i => string.IsNullOrEmpty(converter(i)) == false).Select(i => converter(i))).ToArray()); 
    } 
}

NUnit Test:

[Test] 
public void JoinTest() 
{ 

    string[] joins = new string[]{"1", "2", "3", "4"}; 
    string joinTest = new.Join(",", joins, item => item); 
    Assert.AreEqual("1,2,3,4", joinTest); 

    joins = new string[] { "1", "", "3", "4" }; 
    joinTest = new CollectionUtilities().Join(",", joins, item => item); 
    Assert.AreEqual("1,3,4", joinTest);

    joins = new string[] { "1"}; 
    joinTest = new CollectionUtilities().Join(",", joins, item => item); 
    Assert.AreEqual("1", joinTest);  
}

LINQ Links

I have been looking into LINQ lately and have run across a ton of links that look useful. Unfortunately I have not had much time to actually play with LINQ but I thought I would pass along that links that I have been gathering for others.

Building an RSS Feed using LINQ to XML and LINQ to SQL

LINQ to Entities Web Cast

Nested Selects in LINQ to SQL

The C# ?? null coalescing operator (and using it with LINQ)

LinqDataSouce - DataItem in code behind

Dynamic String based Queries in LINQ - Dynamic Expression API

101 LINQ Samples

Lambda Expressions and Expression Trees

LINQPad (Query Manager using LINQ)

LINQ to SQL Web Cast and LINQ to SQL Web Cast Source Download

A Little LINQ

Hanselminutes Podcast 79 - LINQ to XML

Improving LINQ Code Smell with Explicit and Implicit Conversion Operators

Trying Out Persistence Ignorance with LINQ (Part I)

Trying Out Persistence Ignorance with LINQ (Part II)

DotNetRocks Shows: 270, 271, 274

LINQ To SQL Debugger Visualizer Revisited - Same Concerns Better UI

DataContext.Log - Logging LINQ To SQL Output to Console or Debugger Output Window

LINQ To SQL - Lazy Loading Properties and Specifying PreFetch When Needed - Sweet Query and Performance Control

Code Generation using CodeSmith - v4.1 - Visual Studio 2008 Support and LINQ to SQL Templates

DLinq (Linq to SQL) Performance: Part 1, Part 2, Part 3, Part 4, Part 5