AddMapping列到列表 [英] AddMapping column to list
问题描述
我有以下Student
类,每个类都有一个考试列表,我需要将考试"列映射到Exams_Line_list
的属性.以下代码返回空的Exams_Line_list
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; }
}
}
阅读Excel工作表:
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);
}
模式:
我已将一个String属性添加到映射Exam列(Exams_Line1
)
断点StudentList_c
I have add a String property to map Exam column, (Exams_Line1
)
break point StudentList_c
现在,它读取所有行,包括每个检查值包含重复的ID,但如何将它们分组到定义的列表(Exams_Line1
)
Now it read all rows including duplicate id with each exam value, but how to group them to the defined list (Exams_Line1
)
推荐答案
也许您可以使用它:
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();
在这里,我假设Student_List
具有与excel工作表中的行相对应的所有行,并且Exams_Line1
属性正在映射到Exam
列.
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.
这篇关于AddMapping列到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!