Concatenating values with commas in arbitrary collection

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?

Advertisements

JSLinq Sum method

JSLINQ is an awesome library that attempts to mimic the functionality provided by .NET LINQ libraries in clientside javascript code.

However while it is a great implementation it lacks a Sum method so for anyone interested here is the Sum method for JSLINQ that I created…

Sum: function (clause) {
    var values = this.Select(clause).ToArray();

    if (values != null) {
        var result = 0;

        for (var i = 0; i < values.length; i++) {
            var val = values[i];
            if (!isNaN(parseFloat(val)) && isFinite(val)) {
                result = result + parseInt(val);
            }
        }

        return result;
    } else {
        throw new Error("Argument Null Source");
    }
}