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:
I have add a String property to map Exam column, (Exams_Line1
)
break point StudentList_c
Now it read all rows including duplicate id with each exam value, but how to group them to the defined list (Exams_Line1
)
May be you can just use this:
Here I'm assuming that
Student_List
has all the rows corresponding to rows in excel sheet andExams_Line1
property is being mapped toExam
column.