Daniel Cazzulino's Blog : Friday, January 04, 2008 - Posts

Friday, January 04, 2008 - Posts

foreach to from..select (I)

Build a coma-separated list of the string representation of an array of objects, rendering null values as “null”, string values with quotes ( i.e. 15, true, "foo", null ) and everything else using object.ToString():

foreach:

				List<string> values = new List<string>(invocation.Arguments.Length);
foreach (var x in invocation.Arguments)
{
    values.Add(x == null ?
        "null" :
        x is string ?
            "\"" + (string)x + "\"" :
            x.ToString()
    );
}

string msg = String.Join(", ", values.ToArray());

from..select:

				string msg = String.Join(", ", 
    (from x in invocation.Arguments select x == null ?
        "null" :
        x is string ?
            "\"" + (string)x + "\"" :
            x.ToString()
    ).ToArray()
);

posted Friday, January 04, 2008 4:15 AM by kzu with 1 Comments

/kzu dev↻d