Bill Software Engineer
Bill Software Engineer

Reputation: 7782

In Razor, how do I print a loop with delimiter?

I have the following code:

            @foreach (UserAccount ua in company.Users) {
                @ua.userName, 
            }

Which would print:

user1, user2, user3,

How do I get rid of the last ","?

Upvotes: 4

Views: 489

Answers (2)

Mathew Thompson
Mathew Thompson

Reputation: 56429

Using String.Join:

@(string.Join(company.Users.Select(u => u.UserName), ", "))

Upvotes: 6

Sly
Sly

Reputation: 15217

use String.Join method. it will handle the last comma for you.

@(string.Join(company.Users.Select(x => x.userName), ", "))

Upvotes: 5

Related Questions