On numerous occasions I have needed to concatenate a property of an entity in a collection with a comma and have typically resulted in code that requires a loop for each entity along with a removal of the extra comma once the loop is completed.
This has always been a pain and plain ugly
Today I stumbled across a new overload for String.Join (String.Join Method (String, IEnumerable<String>)) which is only available after .NET 4.0 .
This allowed me to come up with the following extension.
public static string ConcateWith(this IEnumerable<T> values, Func<T, string> property, string separator) { return string.Join(separator, values.Select(o => property(o))); }
Nice eh?