0

How can I load a CSV file into Enum in c#, I want to import data from csv file and put this data into number of enums based on number of lines (record) in csv file. This is my Csv file:
Csv file

I want to import this data in 4 enum such as i create this enums by my hand:

enum Name { Sam, John, Nodir };
enum Job { M.S, M.R, M.d};
enum Name { 5000, 7000, 9000};
enum Name { Some, Data, Info};

How i can make that? Can anyone give me please a link or piece of code

6
  • 3
    You shouldn't really be using enums for this, Either a dictionary or a list depending on how you intend to use it later and if you intend to keep the records associated to one another. Commented Sep 2, 2015 at 6:51
  • Enums are created at design time. Commented Sep 2, 2015 at 7:29
  • Why do you think you need enums? What are you really trying to do? Enums are useful only to represent a limited, predefined, small set of values. Your data is none of these things - in fact it's the exact opposite. Commented Sep 2, 2015 at 7:29
  • @PanagiotisKanavos, You are right but i use enum because i will compare this data by another existing enums to check some values. Commented Sep 2, 2015 at 7:54
  • @mbugr You don't need enums to do that. Just check the values against the enum names using Enum.IsDefined, Enum.GeName or Enum.GetNames. Sounds that you really wanted to ask how to compare a string value to an enum's name? That's what's called the XY problem - asking about supposed solutions to a problem, not the actual problem itself. Commented Sep 2, 2015 at 7:56

1 Answer 1

1
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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

It is best to explain why your solution works, rather than simply placing your code here.

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.