AddMapping column to list

2019-09-05 15:55发布

I have the following Student class, which each ID has a list of exams, I need to map the column of Exam to property of Exams_Line_list. The following code return empty Exams_Line_list

public class Student
    {
        string id;
        public List<String> exams_Line_list = new List<String>();
        public String exams_Line1;

        public string Exams_Line1
        {
            get { return exams_Line1; }
            set { exams_Line1 = value; }
        }

        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        public List<String> Exams_Line_list
        {
            get { return exams_Line_list; }
            set { exams_Line_list = value; }
        }
}

Reading the Excel sheet:

IQueryable<Student> Students_var;
var  excel = new ExcelQueryFactory(fileName_global1);

excel.AddMapping<Student>(x => x.ID, "STU_NO");
excel.AddMapping<Student>(x => x.Exams_Line1, "Exam");
Students_var = from c in excel.Worksheet<Student>("Stu_Schedule")
               select c;

List<Student> StudentList_c = Students_var.ToList();

var grouped = StudentList_c.GroupBy(x => x.ID).Select(x => new Student
              {
                ID = x.Key,
                Exams_Line_list = x.SelectMany(z => z.Exams_Line_list).ToList()
              }).ToList();

foreach (var r in grouped)
{
    Console.WriteLine(r.ID);
        foreach (String s1 in r.Exams_Line_list)
                    Console.WriteLine(s1);
}

Schema:

enter image description here

I have add a String property to map Exam column, (Exams_Line1) break point StudentList_c enter image description here

Now it read all rows including duplicate id with each exam value, but how to group them to the defined list (Exams_Line1)

1条回答
家丑人穷心不美
2楼-- · 2019-09-05 16:13

May be you can just use this:

var grouped = StudentList_c.GroupBy(x => x.ID).Select(x => new Student
{
   ID = x.Key,
   Exams_Line_list = x.Select(z => z.Exams_Line1).ToList()
}).ToList();

Here I'm assuming that Student_List has all the rows corresponding to rows in excel sheet and Exams_Line1 property is being mapped to Exam column.

查看更多
登录 后发表回答