如何从一列写入数组的单独值

时间:2018-09-24 01:09:15

标签: arrays .net csv c#-2.0

static void Main(string[] args)
{
        string[] LinesInFile = File.ReadAllLines("D:\\Book.csv");

        foreach (string line in LinesInFile)
        {
            if (line != "")
            {
                string[] columns = line.Split(',');
                string PatientID = columns[0];
                string DateOfBirth = columns[1];
                string DateFirstSeen = columns[2];
                string DateOfDiagnosis = columns[3];
                string TreatmentStartDate = columns[4];
                string TreatmentEndDate = columns[5];
                string CancerType = columns[6];
                string TreatmentType = columns[7];

                Console.WriteLine(PatientID[0]);
            }
        }
}

这是csv文件中的一些数据,

1,30/07/1966,06/01/2017,21/01/2017,01/02/2017,01/06/2018,4,3
2,25/09/1970,02/01/2017,27/01/2017,04/02/2017,06/05/2018,5,1
3,23/08/1964,11/01/2017,19/01/2017,04/02/2017,31/03/2018,5,1

因此,基本上,当我打印'PatientID'时,它将打印我想要的所有1-3,因此我可以分别打印每个,所以如果我只想打印第一行的话。我当时在考虑使用列表来保存每个“患者”的信息,但是即使那样,如果我需要比较信息,我将如何分别返回每个患者的信息。

3 个答案:

答案 0 :(得分:0)

您可以使用所需的所有属性创建书的类,例如PatientID,DateOfBirth,DateFirstSeen等,然后创建该类的列表(List ),以便可以将数据按行存储在列表中

答案 1 :(得分:0)

应该有所帮助:

public class Patient {
    public string PatientID {get; set;}
    public string DateOfBirth {get; set;}
    public string DateFirstSeen {get; set;}
    public string DateOfDiagnosis {get; set;}
    public string TreatmentStartDate {get; set;}
    public string TreatmentEndDate {get; set;}
    public string CancerType {get; set;}
    public string TreatmentType {get; set;}
}
List<Patient> lstPatients = new List<Patient>();
lstPatients.add(new Patient{PatientID = columns[0],DateOfBirth = columns[1],..v.v});

答案 2 :(得分:0)

您可以创建一类患者来存储每个患者信息。并将所有患者存储在列表中。然后您可以从列表中检索它:

        public class Patient
        {
            public string PatientID;
            public string DateOfBirth;
            public string DateFirstSeen;
            public string DateOfDiagnosis;
            public string TreatmentStartDate;
            public string TreatmentEndDate;
            public string CancerType;
            public string TreatmentType;
        }

        public static List<Patient> LoadPatients(string filePath)
        {
            var list = new List<Patient>();
            string[] LinesInFile = File.ReadAllLines("D:\\Book.csv");

            foreach (string line in LinesInFile)
            {
                if (line != "")
                {

                    string[] columns = line.Split(',');

                    list.Add(new Patient
                    {
                        PatientID = columns[0],
                        DateOfBirth =columns[1],
                        DateFirstSeen = columns[2],
                        DateOfDiagnosis = columns[3],
                        TreatmentStartDate = columns[4],
                        TreatmentEndDate = columns[5],
                        CancerType = columns[6],
                        TreatmentType = columns[7]
                    });
                }
            }

            return list;
        }

        public static Patient GetPatient(List<Patient> patients, string patientId)
        {
            return patients.FirstOrDefault(pt => pt.PatientID.Equals(patientId));
        }


        #endregion
        public static void Main(string[] args)
        {
            var patients = LoadPatients("D:\\Book.csv");
            var patient = GetPatient(patients, "2");
        }