How to separate the selected column from the .CSV file and save them in the new .csv file with the header file in C #


I want to read the CSVfile and those selected columns with headers are save in another new `CSV.

*I want to add following requirement for my code.*please help me to add these for my code.

In here I consider this requirements; Age >=25 && Marks0 ==True save in this name Pass.csv with under header column in this .csv file (Index,Age,Marks0)

other requirement is Age >=20 && Age <=25 & Marks1 ==False save in this name General.csv with under header column in this .csv file (Index,Age,Marks0,Marks1)

others are save in Ignore.csv with headers (Index,Age,Marks0,Marks1,Marks2,Marks3)

this is my sample .CSV file

Index,Age,Marks0,Marks 1,Marks2,Marks3

I created a class to store my column values

class Marks

        public int Index { get; set; } // property to  store Index
        public int Age { get; set; } // property to store Age
        public bool Marks0 { get; set; } // property to store Marks0
        public bool Marks01 { get; set; } // property to store Marks01
        public bool Marks2 { get; set; } // property to store Marks2
        public bool Marks3 { get; set; } // property to store Marks3


Code for reading .CSV file as follows;

private void button1_Click(object sender, EventArgs e)
               List<Marks> Observing1 = new List<Marks>(); // List to store all available Marks objects from the CSV
                Marks statusInt = new Marks();
                // Loops through each lines in the CSV
                foreach (string line in System.IO.File.ReadAllLines(OutputFilePath.Text).Skip(1)) // .Skip(1) is for skipping header
                    // here line stands for each line in the CSV file

                    string[] InCsvLine = line.Split(',');
                    statusInt.Mark0 = (InCsvLine[2] == "TRUE" ? true : false);
                    statusInt.Mark1 = (InCsvLine[3] == "TRUE" ? true : false);
                    statusInt.Mark2 = (InCsvLine[4] == "TRUE" ? true : false);
                    statusInt.Mark3 = (InCsvLine[5] == "TRUE" ? true : false);



Please give any help for solve my problem.

I find multiple questions, but to get you started you can seperate columns from csv like this:

        var recordsToSaveInPassFile = new List<Class>();
        var recordsToSaveInGeneralFile = new List<Class>();

        var lines = File.ReadAllLines(@"C:\temp\data.csv", Encoding.GetEncoding(1252));

        foreach (var line in lines)
            var items = line.Split(';');

            int parsedAge;

            if (int.TryParse(items[0], out parsedAge))
                if(parsedAge > 24)
                   var recordToPass = new Class
                    Age = items[0],
                    item2 = items[1]

                else if(parsedAge > 19)
                   var recordToGeneral = new Class
                    Age = items[0],
                    item2 = items[1]



            Console.WriteLine([email protected]"'{line}': could not be parsed");

This can be done much more nicer, but this might give you an idea of how to check for different values from your csv file.