Ryan Sampson
Ryan Sampson

Reputation: 6817

Convert List<Enum> to List<string>

I have a list of enum values:

public static readonly List<NotifyBy> SupportedNotificationMethods = new List<NotifyBy> {
   NotifyBy.Email, NotifyBy.HandHold };

I would like to output it as a comma separated list. (EG: "Email, Handhold")

What is the cleanest way of doing this?

Upvotes: 8

Views: 10206

Answers (3)

Charles Lambert
Charles Lambert

Reputation: 5132

you can use linq:

string.Join(", ", SupportedNotificationMethods.Select(e => e.ToString());

Upvotes: 4

vcsjones
vcsjones

Reputation: 141638

Perhaps this:

var str = String.Join(", ", SupportedNotificationMethods.Select(s => s.ToString()));

You can read more about the String.Join method at MSDN. Older versions of String.Join don't have an overload that takes an IEnumerable. In that case just call ToArray() after select.

Upvotes: 14

Derek Hunziker
Derek Hunziker

Reputation: 13141

String.Join(", ", SupportedNotificationMethods.Select(e => e.ToString()).ToArray());

Upvotes: 0

Related Questions