tags:

views:

61

answers:

1

Possible Duplicate:
What is the LINQ way to implode/join a string array?

Say I have a linq query that returns a generic list containing a single string element:

//Actually comes from a linq to sql query so I don't actually have the array.
string[] mer = {"cat","dog","fish"};
var k = (from k in mer
         select k);

Is there a quick convenient way to print the results k in a string like: "cat, dog, fish" using projections?

I know I can simply use a foreach loop and += them into a string variable if there isn't a neat way.

+2  A: 

Use String.Join() - it allows you to specify a delimiter, and joins a given collection of strings into a single string joined by that delimiting character / string.

It's not Linq but it solves your problem.

Kirk Broadhurst
Thanks. Ended up using this: `String.Join(", ", k.Select(x => x.Animal).ToArray());`
bill
@bill: You don’t need the `.ToArray()` in C# 4.0 or later.
Timwi