In Razor, how do I print a separator loop?

I have the following code:

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

To print:

 user1, user2, user3, 

How to get rid of the last ","?

+4
source share
2 answers

Using String.Join :

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

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

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

Source: https://habr.com/ru/post/1479745/


All Articles