0

I have Ienumerable<string> collection that I want to concatenate into a single string with delimitor ;

for instance {"One","Two","Three"} -> "One;Two;Three;"

is it possible to do using the following function?

List<string> list = new List<string>(){"One","Two","Three"};
list.Aggregate<String>((x,y) => x + String.Format("{0};",y));

I have tried also this code:

list.Aggregate<String>((x,y) => String.Format("{0};{1}",x,y)); 

both samples didn't work.

EDIT: I see that it is not possible to do what I wanted using Linq-2-sql or Aggregate function in Linq-2-sql.

http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/dac496c0-5b37-43ba-a499-bb8eff178706/

EDIT2: the workaround I used is to go over the items returned by the original linq query...and copies them to a new list and do the join as suggested in the answers below on a linq object and not linq-2-sql object.

3 Answers 3

5

You can just use String.Join for this. If you're using .NET4 then you can use the overload that takes an IEnumerable<string> directly:

string joined = string.Join(";", list);

If you're using an older version of the framework then you'll need to use the overload that takes a string[] array instead, converting your collection to an array first if necessary:

string joined = string.Join(";", list.ToArray());

EDIT...

Of course, if you really want to use Aggregate for some reason then there's nothing stopping you. If so, it's usually recommended to build your string using a StringBuilder rather than multiple string allocations:

string joined = list.Aggregate(new StringBuilder(),
                               (sb, s) => sb.Append(s).Append(';'),
                               sb => (sb.Length > 0) ? sb.ToString(0, sb.Length - 1)
                                                     : "");
Sign up to request clarification or add additional context in comments.

3 Comments

this doesn't work i get the following error down the road: Method 'System.String Join(System.String, System.Collections.Generic.IEnumerable`1[System.String])' has no supported translation to SQL.
@Itay: Sorry, I completely missed your LINQ-to-SQL tag. This will only work with LINQ-to-Objects.
it's ok, i have edited the tag to linq-to-sql because i reliazed it makes a different
2

You can do it using below code

list.Aggregate((i, j) => i + ";" + j);

Comments

1

You'll need to provide an initializer, otherwise the first element will not have a ; added to it:

list.Aggregate<String>("", (x,y) => x + String.Format("{0};",y));

3 Comments

On this code i get the following error: "The query operator 'Aggregate' is not supported."
Microsoft / Joe Albahari: The Aggregate operator is not supported in LINQ to SQL because there is no way to translate arbitrary aggregations to SQL. I can't imagine this will change anytime soon.
Your original question didn't state linq-to-sql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.