ratty
ratty

Reputation: 13444

how add the list of strings into string using linq?

I have List consists of {"a","b","c"} i have string s contains{"alphabets"} .i like to add the list to string. i need final output in s like this `{"alphabetsabc"}. i like to do this using linq.

Upvotes: 5

Views: 1338

Answers (4)

Hans Kesting
Hans Kesting

Reputation: 39338

You need the Aggregate method, if you really want to use LINQ.

Upvotes: 1

LukeH
LukeH

Reputation: 269648

Using LINQ, or even Join, would be overkill in this case. Concat will do the trick nicely:

string s = "alphabets";
var list = new List<string> { "a", "b", "c" };

string result = s + string.Concat(list);

(Note that if you're not using .NET4 then you'll need to use string.Concat(list.ToArray()) instead. The overload of Concat that takes an IEnumerable<T> doesn't exist in earlier versions.)

Upvotes: 8

Vlad
Vlad

Reputation: 35594

Why not just string.Join? Using Linq would be an overkill.

Upvotes: 5

saku
saku

Reputation: 3616

Quick & dirty:

List<string> list = new List<string>() {"a", "b", "c"};
string s = "alphabets";

string output = s + string.Join("", list.ToArray());

Upvotes: 2

Related Questions