[英]LINQ Left Joins - 'System.NullReferenceException' Error
I have been trying to create a LINQ statement that will join 3 tables with left outer joins. 我一直在尝试创建一个LINQ语句,该语句将用左外部联接联接3个表。 When I try to run the code, I am receiving a 'System.NullReferenceException' error.
当我尝试运行代码时,我收到“ System.NullReferenceException”错误。
var model = from con in dbo.Contacts
from conSpe in dbo.Contact_Specialties
.Where(cs => cs.ContactID == con.ContactID)
.DefaultIfEmpty()
from lan in dbo.Languages
.Where(l => l.ContactID == con.ContactID)
.DefaultIfEmpty()
orderby con.familyName
where (searchFirst == null || con.givenName.StartsWith(searchFirst)) &&
(searchLast == null || con.familyName.StartsWith(searchLast))
select new PhysicianDirectory
{
Contact = con,
Contact_Specialty = conSpe,
Language = lan
};
return View(model);
Below is my code for my Direcory.cs Model in which I want to use the PhysicianDirectory class within my View. 以下是我的Direcory.cs模型的代码,我想在其中使用View中的PhysicianDirectory类。
[Table("Contacts")]
public class Contact
{
[Key]
public int ContactID { get; set; }
public string familyName { get; set; }
public string givenName { get; set; }
public string additionalName { get; set; }
public string honorificSuffix { get; set; }
public string title { get; set; }
public string School { get; set; }
}
[Table("Languages")]
public class Language
{
[Key]
public int LanguageID { get; set; }
public int ContactID { get; set; }
public string LanguageSpoken { get; set; }
}
[Table("Contact_Specialties")]
public class Contact_Specialty
{
[Key]
public int ContactID { get; set; }
public int SpecialtyID { get; set; }
public string Specialty1 { get; set; }
public string Specialty2 { get; set; }
}
public class PhysicianDirectory
{
public Contact Contact { get; set; }
public Language Language { get; set; }
public Fellowship Fellowship { get; set; }
public Contact_Specialty Contact_Specialty { get; set; }
}
The 'System.NullReferenceException' error keeps appearing when I try to enter any information from Language class. 当我尝试从Language类输入任何信息时,“ System.NullReferenceException”错误始终出现。 The Language data will appear if I take out .DefaultIfEmpty() but will only show a small portion that is selected within the Language table.
如果我取出.DefaultIfEmpty(),将显示语言数据,但仅显示在语言表中选择的一小部分。 Is there a way for empty space to appear in place of NULL in code so the 'System.NullReferenceException' doesn't appear?
有没有一种方法可以在代码中使用空位置代替NULL,从而不会出现“ System.NullReferenceException”?
@model IEnumerable<MvcDirectory.Models.PhysicianDirectory>
<div id="DirectoryList">
@foreach (var item in Model)
{
<a href='@Url.Action("Details", "Profile", new { id = item.Contact.ContactID })'>
<div class="PhysicianDetails">
<img src="~/Images/Capture.png" alt="Physician IMG" />
<h4>@item.Contact.givenName @item.Contact.additionalName @item.Contact.familyName, @item.Contact.honorificSuffix</h4>
<p>@item.Contact_Specialty.Specialty1</p>
<p>@item.Contact_Specialty.Specialty2</p>
<p>@item.Contact.title</p>
<p>@item.Language.LanguageSpoken</p>
</div>
</a>
}
</div>
Any suggestions? 有什么建议么? Thanks!
谢谢!
Just handle item.Language
being null in your view: 只需处理
item.Language
在您的视图中为空:
<p>@item.Language == null ? "" : @item.Language.LanguageSpoken</p>
In C# 6 this is even simpler: 在C#6中,这甚至更简单:
<p>@item.Language?.LanguageSpoken</p>
(Assuming that null ends up as an empty string in Razor...) (假设null在Razor中最终为空字符串。)
Since it is a Left join you have to handle null for conSpe and lan 由于它是左连接,因此必须为conSpe和lan处理null
var model = from con in dbo.Contacts
from conSpe in dbo.Contact_Specialties
.Where(cs => cs.ContactID == con.ContactID)
.DefaultIfEmpty()
from lan in dbo.Languages
.Where(l => l.ContactID == con.ContactID)
.DefaultIfEmpty()
orderby con.familyName
where (searchFirst == null || con.givenName.StartsWith(searchFirst)) &&
(searchLast == null || con.familyName.StartsWith(searchLast))
select new PhysicianDirectory
{
Contact = con,
Contact_Specialty = (conSpe == null? string.Empty : conSpe),
Language = (lan == null ? string.Empty : lan)
};
string.Empty
should be handled differently based on what that type is. string.Empty
应该根据类型是不同的方式进行处理。 But you get the idea right. 但是,您的想法正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.