Insert comma after checking more than one name

Asked

Viewed 83 times

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 in case you have more names on file.

  • user.Typesmodelaggregate = string. Join(",", user.typesModel);

2 answers

1


Just make a string.Join:

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

This way the concatenation is made by each element.

  • You are welcome, we are here for that! Give an UP on the answer and mark it as valid if it really helped you!

  • Thank you John for your support! This way it was not possible to perform the insertion separated by commas, in this case I have a multiselect combobox, which when selecting more than one value, separate them by comma when printing the new registration in the user table..

  • Then perhaps it is better to go into more detail about your question, otherwise the answers will not agree with what you need.

  • I put the code of my Get method for better understanding!

0

In case I have a multiselect in html,and I want to separate the selected values within the combobox and separate by commas when I list all users with their replies 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;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.