Daniel Cazzulino's Blog : foreach to from..select (I)

Subscriptions

News

Source code published in this blog is public domain unless otherwise specified.

 

kzu in LinkedIn

  Microsoft MVP Profile

 Contact

Post Categories

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 on Friday, January 04, 2008 4:15 AM by kzu

# foreach to from..select (I) @ Friday, January 04, 2008 4:40 AM

Build a coma-separated list of the string representation of an array of objects, rendering null values

Anonymous