I have an object structure that looks like this:
var Results = new List<ResultObj>()
{
new ResultObj()
{
Messages = new List<MessageObj>()
{
new MessageObj()
{
Message = "message 1"
},
new MessageObj()
{
Message = "message 2"
}
}
},
new ResultObj()
{
Messages = new List<MessageObj>()
{
new MessageObj()
{
Message = "message 3"
}
}
}
}
How do I use LINQ or another C# approach to get a single string with all the Message values concatenated together? Something like what's below
"message 1, message 2, message 3"
Thanks!
Use String.Join
and SelectMany
:
String.Join(", ", Results.SelectMany(x=> x.Messages).Select(y => y.Message ));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With