C# Linq联接仅在所有表都有数据时工作

C# Linq联接仅在所有表都有数据时工作,c#,asp.net,linq,join,C#,Asp.net,Linq,Join,下面你可以找到我的问题。我的视图仅在同时填写“DocentEnCompetities”和“DocentenLocaties”时循环浏览数据。这是一个问题,因为我希望能够循环它们,即使其中一个没有任何数据 var ShowCompetenties = from d in db.Docent join dc in db.DocentenCompetenties on d.DocentID equals dc.DocentID join c in db.Competenties

下面你可以找到我的问题。我的视图仅在同时填写“DocentEnCompetities”和“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
        }
}
if (@item.DocentenCompetenties != null){}
if (@item.DocentenLocaties!= null){}
能力
@foreach(模型中的var项目)
{
如果(@item.docentencompenties!=null&&@item.docentencompenties.DocentID.ToString()!=null){
@item.competites.Name@Html.ActionLink(“删除”、“删除竞争对手”,新的{id=item.docentencompenties.docentencompentiesid})
}
医生
@foreach(模型中的var项目)
{
如果(@item.docentnlocaties!=null&&@item.docentnlocaties.DocentID.ToString()!=null)
{
@item.Locaties.Name
}
}

您正在以这种方式进行内部联接

听起来你真的想做一个外部连接。 试试这个:

var ShowCompetenties = from d in db.Docent
  join g1 in db.DocentenCompetenties on d.DocentID equals g1.DocentID into group1
  from dc in group1.DefaultIfEmpty()

  join g2 in db.Competenties on dc.CompetentiesID equals g2.CompetentiesID into group2
  from c in group2.DefaultIfEmpty()

  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, Competenties = c, DocentenCompetenties = dc, DocentenLocaties = dl, Locaties = l };
但是,如果希望进行完全外部联接,则这只是一个左外部联接。必须先进行左外部联接,然后进行右外部联接,最后将两者合并(使用
Union()

编辑 根据评论,关于您在
联合后遇到的错误

    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, Locaties = null, DocentenCompetenties = dc, DocentenLocaties = null};


    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, Competenties = null, Locaties = l, DocentenCompetenties = null, DocentenLocaties = dl };



   var ShowCompetenties = LeftShowCompetenties.Union(RightShowCompetenties);
(检查构造函数中添加的
Locaties=null
Competenties=null

查看

<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
        }
}
if (@item.DocentenCompetenties != null){}
if (@item.DocentenLocaties!= null){}

如果已设置导航属性,则左键连接更容易:

var ShowCompetenties =
  from d in db.Docent
  where d.DocentID == id
  from dc in d.DocentenCompetenties.DefaultIfEmpty()
  let c = dc.Competenty
  from dl in d.DocentenLocaties.DefaultIfEmpty()
  let l = dl.Locaty
  select new ShowCompetenties {
    Docenten = d,
    Competenties = c,
    DocentenCompetenties = dc,
    DocentenLocaties = dl,
    Locaties = l };

看看我在使用@foreach(模型中的var item){@item.Locaties.Name}的时候,我尝试了一下,但是失败了。我应该重写查询吗?不用说,如果你必须在LINQ中进行连接,那么可能会出问题——至少,实体之间缺少关系。最糟糕的情况是,LINQ被用作SQL的替代品(不是)并且整个连接应该替换为一个视图。至少,在模型/映射中创建适当的关系检查:它提供了关于所有这些的更多解释。对于创建正确的连接:您基本上可以在连接中切换表。如果您希望了解更多详细信息,这里有很多示例。键入“DocentenAlumniAPP.Mod”els.ShowCompetities'出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中,我在下面的示例中遇到了一个错误,听起来您选择的构造函数在左连接和右连接中不同。(该错误通常在
联合()期间引发。
)您能发布完整的查询吗?啊,是的,您“必须”使用类似的构造函数。我将用一个应该有效的示例更新我的答案。编辑:更新它。@SaraBarreraRiano-此时发生错误,因为item.docentEncompeties.DocentID不能使用ToString()方法。您的基本要求是将DocentID转换为字符串,如果为null,则无法执行此操作。在检查是否为null之前,它会尝试执行此操作。去掉ToString()对于此检查,我所拥有的联接正在工作,我想,我正在与此错误进行斗争,现在在App_Web_q05ilzt1.dll中发生了类型为“System.NullReferenceException”的异常,但未在user中处理code@SaraBarreraRiano如果必须使用联接,那么LINQ映射将被破坏。如果必须使用联接,则使用ORM没有任何好处手动创建实体,但这会导致很多问题-关系代码以连接的形式分布在所有控制器上,而在DbContext的配置中,关系代码应该是几行代码,所以这是一个更好的解决方案?