http://blogs.clariusconsulting.net/kzu

Daniel Cazzulino's Blog

Go Back to
kzu′s Latest post

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()
);


Comments

1 Comment

  1. This looks interesting. I am currently looking to make a site for a museum. I am stymied. I need a way to list a items with the same size, color, content (e.g., birds, cats, chairs, etc.),. I am not at all schooled, so I do not know if what you have posted is part of a whole needed to do that function. Is what you are saying what I am looking for?