CsvHelper try to export in csv a list which contain another list .net

爱⌒轻易说出口 提交于 2020-02-02 12:54:07

问题


I implement a web api in .Net Framework using csvHelper to export a csv.

I have the classes

public class StudentInfo
{
    public Student Student {get;set;}
    public Courses[] Courses { get; set; }
}

public class Student
{
    public string Id {get;set;}
    public string Name{ get; set; }
}

public class Course
{
   public string CourseId {get;set;}
   public string CourseName{ get; set; }
}

I have a method which return a List

public List<StudentInfo> FetchStudentInfo()
{
    //bla bla 
}

Now I want to export in a csv the List using the csvHelper

here is my code

List<StudentInfo> records = _service.FetchStudentInfo()
    using (StreamWriter sw = new StreamWriter(memoryStream))
    {
        using (var csv = new CsvWriter(sw))
        {        
            csv.WriteRecords(records);
            sw.Flush();
        }
    }

but this code export to csv only the object Students. How can export to csv for each student to have the list with the courses in the same line?

For example 12 jim java c# python

if i try

foreach(var item in records)
{
    csvWriter.WriteRecords(item.Student);//I have compile error because it want to pass an IEnumerable
    csvWriter.WriteRecords(item.Courses);
}

回答1:


@dimmits answer works just fine and is easy to read. I thought I would provide another option that is more configuration based. It does require a modification to the StudentInfo class.

public class Program
{
    public static void Main(string[] args)
    {
        var studentInfos = FetchStudentInfo();

        using (var csv = new CsvWriter(Console.Out))
        {
            csv.Configuration.HasHeaderRecord = false;
            csv.Configuration.RegisterClassMap<StudentInfoMap>();

            csv.WriteRecords(studentInfos);
        }

        Console.ReadKey();
    }

    public static List<StudentInfo> FetchStudentInfo()
    {
        Course[] courses1 = { new Course { CourseId = "1", CourseName = "java" }, new Course { CourseId = "2", CourseName = "c#" }, new Course { CourseId = "3", CourseName = "python" } };
        Course[] courses2 = { new Course { CourseId = "1", CourseName = "java" }, new Course { CourseId = "2", CourseName = "c#" } };
        return new List<StudentInfo>
        {
            new StudentInfo{ Student = new Student{Id = "12", Name = "Jim"}, Courses = courses1 },
            new StudentInfo{ Student = new Student{Id = "45", Name = "Dave"}, Courses = courses2 }
        };
    }
}

public class StudentInfoMap : ClassMap<StudentInfo>
{
    public StudentInfoMap()
    {
        References<StudentMap>(m => m.Student);
        Map(m => m.CourseNames).Index(3);
    }
}

public class StudentMap : ClassMap<Student>
{
    public StudentMap()
    {
        Map(m => m.Id);
        Map(m => m.Name);
    }
}

public class StudentInfo
{
    public Student Student { get; set; }
    public Course[] Courses { get; set; }
    public string[] CourseNames { get { return Courses.Select(c => c.CourseName).ToArray(); } }
}

public class Student
{
    public string Id { get; set; }
    public string Name { get; set; }
}

public class Course
{
    public string CourseId { get; set; }
    public string CourseName { get; set; }
}



回答2:


CSV format cannot represent a list in a list. I didn't figure how the data can be interpreted when reading the CSV after export.

XML or JSON must be prefer to export complex data into a flat file.

If you really want to get as a CSV file, you must use 2 different separator to distinct the 2 class (by example: the ',' for StudentInfo and ';' for the courses).

After I will create a class for represent CSV data:

public class StudentInfoCsv
{
    public Student Student {get;set;}
    public string Courses { get; set; }
}

List<StudentInfoCsv> csvData = new List<StudentInfoCsv>
foreach(var item in records)
{
    StudentInfoCsv.Add(new StudentInfoCsv()
    {
        Student = item.Student,
        Courses = CsvSerializer.SerializeToCsv(item.Courses)
    }
}
csvWriter.WriteRecords(csvData);



回答3:


the solution is

                       foreach (StudentInfo item in records)
                        {
                            csvWriter.WriteField(item.Student.Id);
                            csvWriter.WriteField(item.Student.Name);

                            foreach (var row in item.Courses)
                            {
                                csvWriter.WriteField(row.CourseName);
                            }
                            csvWriter.NextRecord();
                        }

and the output will be

12 jim java c# python



来源:https://stackoverflow.com/questions/58950513/csvhelper-try-to-export-in-csv-a-list-which-contain-another-list-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!