我有两张桌子。我的主表叫Student
,副表是Marks
。Dapper 对我来说是新的。
这是我的代码:
s_id
是表中的主键,是Student
表中的外键Marks
(学生(父)-标记(子))。
控制器:加入
public ActionResult Index()
{
string sql ="SELECT TOP 10 * FROM Student AS A INNER JOIN Mark AS B ON A.S_ID = B.S_ID";
using (SqlConnection connection = new SqlConnection(connectionstring))
{
var studentDictionary = new Dictionary<int,Student>();
var list = connection.Query<Student,Marks,Student>
(
sql,(student, marks) =>
{
Student Entry;
if (!studentDictionary.TryGetValue(student.S_ID,out Entry))
{
Entry = student;
Entry.Marks = new List<Marks>();
studentDictionary.Add(Entry.S_ID, Entry);
}
Entry.Marks.Add(marks);
return Entry;
},
splitOn:"S_ID")
.Distinct()
.ToList();
ViewBag.list = list;
}
return View();
}
结果.cs
加入模型
public class Result
{
public string S_Name { get; set; } //student.cs
public string S_LName { get; set; } //students.cs
public int Score { get; set; } //marks.cs
public string Status { get; set; } //marks.cs
}
如何使用 result.cs 类访问学生和标记表列?它只访问学生列为什么标记表列不访问视图侧?
我该如何解决这个问题?
看法:
@model IEnumerable<Dapper2Join.Models.Result>
@{
@foreach (var per in ViewBag.list)
{
<tr>
<td>@per.S_Name</td>
<td>@per.S_LName</td>
<td>@per.Score</td>
<td>@per.Status</td>
</tr>
}