csvhelper尝试在csv中导出包含另一个列表.net的列表 [英] CsvHelper try to export in csv a list which contain another list .net

查看:96
本文介绍了csvhelper尝试在csv中导出包含另一个列表.net的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用csvHelper在.Net Framework中实现Web api来导出csv.

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

我有课程

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 
}

现在我想使用csvHelper在csv中导出列表

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

这是我的代码

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

,但是此代码仅将对象Student导出到csv.如何导出到csv,以使每个学生的课程列表都在同一行中?

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?

例如12个jim java c#python

如果我尝试

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

推荐答案

@dimmits答案很好用,很容易阅读.我以为我会提供另一个基于配置的选项.它确实需要修改 StudentInfo 类.

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

这篇关于csvhelper尝试在csv中导出包含另一个列表.net的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆