How to join four tables using Linq in asp.NET

本文关键字:Linq in asp NET using tables to join four How | 更新日期: 2023-09-27 18:28:49

我的查询有问题。我无法加入我的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};

我有两个连接表=DocentLocalities和DocentnCompetenties

.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 })
}

How to join four tables using Linq in asp.NET

设置导航属性,然后:

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 })
}