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

后端 未结 7 2060
一向
一向 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:17

    How about:

    public static string Concat(this IEnumerable source) {
        StringBuilder sb = new StringBuilder();
        foreach(string s in source) {
            sb.Append(s);
        }
        return sb.ToString();
    }
    

    and:

    string s = data.Concat();
    

    This then has no need for the extra ToList() / ToArray() step.

提交回复
热议问题