【问题标题】:How to write separate values of the array from one column如何从一列中写入单独的数组值
【发布时间】:2018-09-24 01:09:14
【问题描述】:
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 全部打印我想要的,所以我可以单独打印每个,所以如果我只想打印第一行,我可以。我正在考虑使用一个列表来保存每个“患者”信息,但即便如此,如果我需要比较信息,我将如何单独返回每个患者的信息。

【问题讨论】:

  • 在解析 csv 文件时创建一个包含患者信息的类,将每个行条目保存为 Patient 类的一个实例,然后将该实例添加到患者变量列表中,您可以使用该变量来检索每个患者的个人信息您的需求

标签: arrays .net csv c#-2.0


【解决方案1】:

您可以创建一个包含您需要的所有属性的书籍类,例如 PatientID 、DateOfBirth 、 DateFirstSeen 等,然后创建该类的列表 (List),以便您可以将数据逐行存储在列表中.

【讨论】:

    【解决方案2】:

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

            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");
            }
    

    【讨论】:

    • 如何在行中打印不同的患者信息,我尝试打印“患者”,但它只返回项目名称和其他一些无用信息。
    【解决方案3】:

    应该有帮助:

    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});
    

    【讨论】:

    • 这将导致编译时错误Patient 不包含采用 x 参数的构造函数。更新您的代码。
    猜你喜欢
    • 2020-11-03
    • 1970-01-01
    • 2021-02-28
    • 2017-12-19
    • 2019-09-03
    • 2012-07-24
    • 2014-06-28
    • 1970-01-01
    • 2021-08-13
    相关资源
    最近更新 更多