LINQ左连接 - 'System.NullReferenceException'错误

时间:2015-04-22 14:33:33

标签: c# asp.net-mvc-4 linq-to-sql

我一直在尝试创建一个LINQ语句,它将连接带有左外连接的3个表。当我尝试运行代码时,我收到'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);

下面是我的Direcory.cs模型的代码,我想在我的视图中使用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; }
}

当我尝试从Language类输入任何信息时,会出现'System.NullReferenceException'错误。如果我取出.DefaultIfEmpty(),将显示语言数据,但只会显示在语言表中选择的一小部分。有没有办法让空格在代码中出现代替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>

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:2)

只需在视图中处理item.Language为空:

 <p>@item.Language == null ? "" : @item.Language.LanguageSpoken</p>

在C#6中,这更简单:

 <p>@item.Language?.LanguageSpoken</p>

(假设null在Razor中以空字符串结尾......)