Enter comma after checking more than one name

1
foreach (var user in users)
{
    var strTypesModel = "";
    foreach (var typesModel in user.typesModel)
    {
        strTypesModel = strTypesModel + typesModel.Name;
    }

    user.TypesModelAggregate = strTypesModel;
}

I want to insert a comma if you have more names added.

    
asked by anonymous 18.09.2018 / 16:31

2 answers

1

Just do a string.Join :

foreach (var user in users)
    user.TypesModelAggregate = string.Join(",", user.typesModel.Name);

This way the concatenation is performed by each element.

    
18.09.2018 / 16:43
0

In case I have a multiselect in html, and I want to separate the selected values inside the combobox and separate by commas the time I list all users with their responsive types of requests.

  public IEnumerable<UserDataModel> Get()
    {
        UserDataModel userModel = new UserDataModel();
        //var reg = ctx.User.Include(x => x.Area.Name).Include(x =>x.RequestType);


        var users = ctx.User.OrderBy(u => u.Name).Select(u => new 
                        UserDataModel
        {
            typesModel = u.RequestType.Select(x => new RequestTypeModel
            {
                Id = x.IdRequestType,
                Name = x.Name
            }).ToList()


        }).ToList();

        foreach (var user in users)
        {
            var strTypesModel = "";
            foreach (var typesModel in user.typesModel)
            {
                strTypesModel = strTypesModel + typesModel.Name;

                user.TypesModelAggregate = string.Join(",", user.typesModel);
            }

        }
     return users;
    }
    
18.09.2018 / 18:09