Wednesday, 31 July 2013

List<string> to string

var strings = new List<string> { "blah", "de", "hoo", "ha" };

var result = strings.Aggregate((i, j) => i + " " + j);

Console.Write(result);
image001

And with a complex type, where a response has a collection of messages, each with a text property:

var messages = this.Response
.Messages
.Select(m => m.Text)
.Aggregate((i, j) => i + " " + j);

2 comments:

  1. Or you could do.....

    var strings = new List { "blah", "de", "hoo", "ha" };

    var result = String.Join(" ",strings);

    Console.Write(result);

    ReplyDelete
  2. Thanks Anon, I prefer your example when dealing with simple strings

    ReplyDelete