I have a class called user that contains a list of groups that are strings ( Group A, Group B, Group C)
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public User()
{
Groups = new List<string>();
}
}
I am then using a json deserializer to create the list of users and groups. However I would like to be able to get a string of the groups with this format for each user:
“Group A”, “Group B”, “Group C”
I have tried this:
string[] AllGroups;
AllGroups = (string[])usrList[0].Groups.ToArray();
return string.Join(",", AllGroups);
However it is giving me a list in this format (with no quotes):
Group A, Group B, Group C
Any idea what I am doing wrong here?
Well you’re not adding the quotes anywhere – so you’re not getting them.
You can surround each item with quotes yourself easily enough:
That’s assuming you’re using .NET 4 or higher – if you’re using .NET 3.5 (which doesn’t have quite as good
string.Joinsupport) you need to create an array, but you don’t need to cast it tostring[](asToArrayalready returns an array)…I’ve added a space after the comma delimiter as well, given your question – I suspect your current code is really giving you
Group A,Group B,Group C.