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#?

user576700
  • 591
  • 1
  • 6
  • 15

4 Answers4

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)
Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Thank you, you brought me an idea to make it more generic: csv.Split(',').Select(s => (T)Enum.Parse(typeof(T), s)).ToList(); – user576700 Jul 19 '13 at 13:39
  • @user576700 idea is already out there - you can create [generic enum class](http://stackoverflow.com/a/13248913/470005) Updated answer :) – Sergey Berezovskiy Jul 19 '13 at 13:40
1
csv.Split(',').Select(x => (Events)int.Parse(x)).ToList();
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
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));
    }
Jag
  • 723
  • 8
  • 21
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()
                  });
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 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 Jul 21 '13 at 15:14
  • @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... – ΩmegaMan Jul 22 '13 at 17:25