我的查询有问题。 我无法加入我的4张桌子。我做错了什么?
var id = Convert.ToInt32(Session["id"]);
var ShowCompetenties = from d in db.Docent
join dc in db.DocentenCompetenties on d.DocentID equals dc.DocentID
where dc.DocentID == id
join c in db.Competenties on dc.CompetentiesID equals c.CompetentiesID
join dl in db.DocentenLocaties on d.DocentID equals dl.DocentID
where dl.DocentID == id
join l in db.Locaties on dl.LocatieID equals l.LocatieID
select new ShowCompetenties { Docenten = d, Competenties = c, DocentenCompetenties = dc, Locaties = l, DocentenLocaties = dl};
我有两个联结表= DocentenLocaties和DocentenCompetenties
.cshtml
<h4>My Locations</h4>
@foreach (var item in Model)
{
@item.Locaties.Name @Html.ActionLink("Delete", "DeleteLocaties", new { id = item.DocentenLocaties.DocentenLocatieID })
}
<h4>My Competences</h4>
@foreach (var item in Model)
{
@item.Competenties.Name @Html.ActionLink("Delete", "DeleteCompetenties", new { id = item.DocentenCompetenties.DocentenCompetentiesID })
}
答案 0 :(得分:2)
设置导航属性,然后:
var id = Convert.ToInt32(Session["id"]);
var ShowCompetenties = db.Docent
.Include(d=>d.Competenties)
.Include(d=>d.Locaties)
.First(d=>d.DocentID==id);
HTML:
@model Docent
<h4>My Locations</h4>
@foreach (var item in Model.Locaties)
{
@item.Name @Html.ActionLink("Delete", "DeleteLocaties", new { id = item.LocatieID })
}
<h4>My Competences</h4>
@foreach (var item in Model.Competenties)
{
@item.Name @Html.ActionLink("Delete", "DeleteCompetenties", new { id = item.CompetentiesID })
}