【发布时间】:2016-04-08 01:14:39
【问题描述】:
您可以在下面找到我的问题。当“DocentenCompetenties”和“DocentenLocaties”都被填写时,我的视图只会循环遍历数据。这是一个问题,因为即使其中一个没有任何数据,我也希望能够循环它们
var ShowCompetenties = from d in db.Docent
join dc in db.DocentenCompetenties on d.DocentID equals dc.DocentID
join c in db.Competenties on dc.CompetentiesID equals c.CompetentiesID
join dl in db.DocentenLocaties on d.DocentID equals dl.DocentID
where d.DocentID == id
join l in db.Locaties on dl.LocatieID equals l.LocatieID
select new ShowCompetenties { Docenten = d, Competenties = c, DocentenCompetenties = dc, DocentenLocaties = dl, Locaties = l };
更新
当前错误: App_Web_xp01otas.dll 中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理 附加信息:对象引用未设置为对象的实例。
var id = Convert.ToInt32(Session["id"]);
var LeftShowCompetenties = from d in db.Docent
join g1 in db.DocentenCompetenties on d.DocentID equals g1.DocentID into group1
from dc in group1.DefaultIfEmpty()
join c in db.Competenties on dc.CompetentiesID equals c.CompetentiesID
where d.DocentID == id
select new ShowCompetenties { Docenten = d, Competenties = c, DocentenCompetenties = dc};
var RightShowCompetenties = from d in db.Docent
join g3 in db.DocentenLocaties on d.DocentID equals g3.DocentID into group3 from dl in group3.DefaultIfEmpty()
where d.DocentID == id
join l in db.Locaties on dl.LocatieID equals l.LocatieID
select new ShowCompetenties { Docenten = d, Locaties = l, DocentenLocaties = dl };
var ShowCompetenties = LeftShowCompetenties.Union(RightShowCompetenties);
查看
<h4>Competenties</h4>
@foreach (var item in Model)
{
if(@item.DocentenCompetenties != null && @item.DocentenCompetenties.DocentID.ToString() != null) {
@item.Competenties.Name @Html.ActionLink("Delete", "DeleteCompetenties", new { id = item.DocentenCompetenties.DocentenCompetentiesID })
}
<h4>Docenten</h4>
@foreach (var item in Model)
{
if(@item.DocentenLocaties != null && @item.DocentenLocaties .DocentID.ToString() != null)
{
@item.Locaties.Name
}
}
【问题讨论】:
-
我正在使用 @foreach(var item in Model) { @item.Locaties.Name } 我尝试但它失败了。我应该重写查询吗?
-
不言而喻,如果您必须在 LINQ 中进行联接,则可能有问题 - 至少,实体之间缺少关系。最坏的情况是,LINQ 被用作 SQL 的替代品(它不是),并且整个连接都应该用 VIEW 代替。至少,在您的模型/映射中创建适当的关系