5

I have defined enum events:

public enum Events {
  UNLOCK = 1,
  LOCK = 2
}

as well as CSV string:

var csv = "1,2";

What would be preferable way to convert csv string to List< Events> in C#?

4 Answers 4

16
csv.Split(',').Select(s => (Events)Enum.Parse(typeof(Events), s));

BTW with generic enum class you can parse this way Enum<Events>.Parse(s) and whole code will look like:

csv.Split(',').Select(Enum<Events>.Parse)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, you brought me an idea to make it more generic: csv.Split(',').Select(s => (T)Enum.Parse(typeof(T), s)).ToList();
@user576700 idea is already out there - you can create generic enum class Updated answer :)
1
csv.Split(',').Select(x => (Events)int.Parse(x)).ToList();

1 Comment

Probably because your answer doesn't return a list. Just a guess.
0

A more verbose way:

    var csv = "2,1,1,2,2,1";

    List<Events> EventList = new List<Events>();

    foreach (string s in csv.Split(','))
    {
        EventList.Add((Events)Enum.Parse( typeof(Events), s, true));
    }

Comments

0

If your whole CSV line is not just events, use regex to parse out the line into its components. Here we have a mixed CSV line and regex can break out each of the values using named match captures. From there we parse it using linq into a new projection for each line of data found.

var csvLine = "1245,1,2,StackOverFlow";
var pattern = @"
^                          # Start at the beginning of each line.
(?<IDasInteger>[^,]+)      # First one is an ID
(?:,)                      # Match but don't capture the comma
((?<Events>[^,]+)(?:,)){2} # 2 Events
(?<Summary>[^\r\n]+)       # Summary text";

var resultParsed = Regex.Matches(csvLine, pattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline)
                        .OfType<Match>()
                        .Select (mt => new {

                        ID = int.Parse (mt.Groups["IDasInteger"].ToString()),
                        Event = mt.Groups["Events"].Captures
                                                   .OfType<Capture>()
                                                   .Select (cp => (Events)Enum.Parse(typeof(Events),  cp.Value))),
                        Summary = mt.Groups["Summary"].ToString()
                  });

2 Comments

mt.Groups["Events"], doesn't it look as a magic strings hell :), just thinking why so much code when @lazyberezovsky answered pretty much well :)
@user576700 I have dealt with CSV files which had multiple columns of data. By specifying a named captures, I ensured that the right data went into the right place upon processing. For your situation, no it makes no sense, but for more robust situation where data acquired is different in type and quantity, the regex named capture for each line can be a very good pick. As to the other answers, I thought most people like to be able to choose between different scenarios. I know I do...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.