C# 在C中导入CSV文件#将字符串列表转换为特定的类类型列表

C# 在C中导入CSV文件#将字符串列表转换为特定的类类型列表,c#,casting,C#,Casting,首先我必须提到,我在编程方面不是很好,但我尽了最大的努力。 以下情况:我将几个csv文件导入到字符串列表中。现在我想将这些列表转换为我需要的classdatatype。例如,在studentlistmanager类中,我改进了列表,并试图将其转换为列表,但这似乎不是那么容易,我尝试创建该列表的对象并将该对象添加到学生列表中,但也不起作用。而不是将System.String[]值放入列表中 internal void ImportStudentList(CsvImportService csv)

首先我必须提到,我在编程方面不是很好,但我尽了最大的努力。 以下情况:我将几个csv文件导入到字符串列表中。现在我想将这些列表转换为我需要的classdatatype。例如,在studentlistmanager类中,我改进了列表,并试图将其转换为列表,但这似乎不是那么容易,我尝试创建该列表的对象并将该对象添加到学生列表中,但也不起作用。而不是将System.String[]值放入列表中

internal void ImportStudentList(CsvImportService csv)
        {
            List<string> newList = new List<string>();
           csv = new CsvImportService("Klassenlisten_20160906.csv");
           for(int i = 0; i <= csv.ClassList.Count;i++)
            {
                for(int x = 0; x <= csv.ClassList.Count;x++)
              {
                    string line = csv.ClassList[i];
                    //  Student st = new Student(line);
                    //  ListOfStudents.Add(st);
                    newList.Add(line);
                    ListOfStudents = newList.Cast<Student>().ToList();

              }
            }
        }
内部无效导入学生列表(CsvImportService csv)
{
List newList=新列表();
csv=新的CsvImportService(“Klassenlisten_20160906.csv”);

对于(int i=0;i这就是您要查找的内容吗?将csv文件的数据保存在学生文件夹中 名单

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace Stackoverflow_Konsole_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string CSV_FILE_PATH = @"filepath.csv";

            List<string> FULL_CSV_STRING = new List<string>();
            Students Student1 = new Students();

            FULL_CSV_STRING.Add(File.ReadAllText(CSV_FILE_PATH));

            foreach (string line in FULL_CSV_STRING)
            {
                Student1.add(line);               
            }

            foreach (string line in Student1.getlist())
            {
                Console.WriteLine(line);
            }
            Console.ReadLine();
        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stackoverflow_Konsole_Test
{
    class Students
    {
        private List<string> List_of_students = new List<string>();


        public Students()
        {
            //constructor
        }

        public void add(string line)
        {
            this.List_of_students.Add(line);
        }         
        public List<string> getlist()
        {
            return this.List_of_students;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
名称空间堆栈溢出测试
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串CSV_FILE_PATH=@“filepath.CSV”;
完整列表\u CSV\u字符串=新列表();
学生1=新生();
完整的CSV字符串.Add(File.ReadAllText(CSV文件路径));
foreach(全字符串中的字符串行)
{
学生1.添加(行);
}
foreach(Student1.getlist()中的字符串行)
{
控制台写入线(行);
}
Console.ReadLine();
}
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间堆栈溢出测试
{
班级学生
{
学生的私人名单=新名单();
公立学生()
{
//建造师
}
公共空添加(字符串行)
{
此。列出所有学生。添加(行);
}         
公共列表getlist()
{
返回此。列出所有学生;
}
}
}

问题在于学生列表不是字符串列表,而是学生类型的列表,有了字符串列表,我可以轻松导入它