Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a LINQ equivalent of string.Join(string, string[]) [duplicate]

Is there any way to convert a collection of objects into a single new object using LINQ?

I want to use this within another LINQ to SQL expression.

like image 430
Matthew Dresser Avatar asked Jun 01 '09 12:06

Matthew Dresser


2 Answers

Why don't you use the string.Join itself?

string.Join("<br/>", collection.Select(e => e.TextProp).ToArray());
like image 176
bruno conde Avatar answered Sep 21 '22 15:09

bruno conde


You can use the Aggregate method...

var myResults = (from myString in MyStrings
                 select myString)
                .Aggregate(string.Empty, (results, nextString) 
                   => string.Format("{0}<br />{1}", results, nextString));

or

var myResults = MyStrings.Aggregate(string.Empty, (results, nextString) 
                   => string.Format("{0}<br />{1}", results, nextString));
like image 38
Scott Ivey Avatar answered Sep 20 '22 15:09

Scott Ivey