I have these code :
var result = new Collection<object>();
result.Add(list.Select(s => new
{
s.ID,
s.Users
})
);
Users is a collection which means it can contains multiple names for example "John", "Rick", "Tom"
, etc. I want to coalsescing it into one string
"John, Rick, Tom"
. Any idea how to achieve this result?
Thank you
UPDATE :
Answer
var result = new Collection<object>();
result.Add(list.Select(s => new
{
s.ID,
Users = string.Join(",", s.Users)
})
);
Directly from your code, it's easier to just go to
result.Add(list.Select(s => new
{s.ID, string.Join(",",s.Users)}));
That should return you a list of pairs with ID, and joined strings of users delimited by ",".
If Users is a List, you can use the following code:
var result = new Collection<object>();
result.Add(list.Select(s => new
{
s.ID,
string.Join(",", s.Users.ToArray())
})
);
Also, you can check this similar question : C# List to string with delimiter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.