I've got a List<Users>
- Users have a Username property.
What I want to know is - is there an better way to get a List<string>
of all the Usernames than to simply loop through and build up my new list?
Use LINQ:
List<string> usernames = users.Select(u => u.UserName).ToList();
Like this:
List<string> userNames = users.ConvertAll(u => u.UserName);
Note that the userNames
list will not reflect subsequent changes to the users
or their UserName
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With