Fastest way to convert a list of strings into a single concatenated string?

后端 未结 7 2086
一向
一向 2021-02-14 21:27

I have some LINQ code that generates a list of strings, like this:

var data = from a in someOtherList
           orderby a
           select FunctionThatReturnsS         


        
7条回答
  •  滥情空心
    2021-02-14 22:00

    Have you tried String.Join? If you're already willing to take the overhead of a .ToList call then instead use .ToArray() and combine it with a call to String.Join.

    var joined = String.Concat(someQuery.ToArray());
    

    Note: My solution is likely not the fastest as it involves a bit of overhead in the array. My suspicion is that it would be faster to go more Marc's route. But in most cases if you're just looking for the quick and dirty way to do it in code, my route will work.

提交回复
热议问题