C# 对象引用错误-Linq

C# 对象引用错误-Linq,c#,linq,entity-framework,C#,Linq,Entity Framework,我是Linq和实体框架的新手,在此查询中收到一个空引用异常: using (var db = new PhoenixContext()) { tblCustomer cust = (from c in db.tblCustomer where c.CustomerID == _CustomerID select c).FirstOrDefault(); string altPhone

我是Linq和实体框架的新手,在此查询中收到一个空引用异常:

using (var db = new PhoenixContext())
{
    tblCustomer cust = (from c in db.tblCustomer
                        where c.CustomerID == _CustomerID
                        select c).FirstOrDefault();

    string altPhone;
    altPhone = cust.tblCustomerContactInformation1.tblCustomerContactInformationPhone.Where(p => p.PhoneTypeID == 2).FirstOrDefault().Phone;
)
tblCustomerContactInformationPhone中只有一行。Where子句应该删除它,我应该得到一个空字符串。然而,我收到的却是:

Object reference not set to an instance of an object.
我做错了什么?如何正确地执行此操作,以便将空结果集正确地转换为空字符串


链接的问题没有帮助,因为这是特定于Linq的使用,而该问题没有涵盖@evanmcdonnal的回答很有帮助,解决了我的问题。

这可能会结束,因为你的问题重复了之前提出的10000000000个其他NullReferenceException问题

考虑一下这个
。其中(p=>p.PhoneTypeID==2)。FirstOrDefault().Phone

如果
tblcustomecontactinformationphone
中没有
PhoneTypeID
为2的项目,会发生什么情况?FirstOrDefault为您提供“default”,在本例中为
null
,然后您执行
从FirstOrDefault返回的值,该值将调用.Phone
,并获得一个NullReferenceException

而是把它分成两行

string phone = String.Empty;
var temp = cust.tblCustomerContactInformation1.tblCustomerContactInformationPhone.Where(p => p.PhoneTypeID == 2).FirstOrDefault();

if (temp != null)
    phone = temp.Phone;
编辑:另一个选项是使用select来获取
Phone
的值,这样您就可以像尝试一样使用空合并操作符。看起来是这样的

var phone = cust.tblCustomerContactInformation1.tblCustomerContactInformationPhone.Where(p => p.PhoneTypeID == 2).Select(x => x.Phone).FirstOrDefault() ?? String.Empty;
这很好,因为如果where没有返回任何元素,select中的lambda表达式将应用于0个元素,因此您最终只能从FirstOrDefault返回null,在这种情况下,您将获取空字符串。

在本子句中:

cust.tblCustomerContactInformation1.tblCustomerContactInformationPhone.Where(p => p.PhoneTypeID == 2).FirstOrDefault().Phone ?? String.Empty;
您正在访问可能为空引用的
Phone
属性

您可以执行以下操作:

var contactinfo = cust.tblCustomerContactInformation1.tblCustomerContactInformationPhone.FirstOrDefault(p => p.PhoneTypeID == 2);

if(contactinfo != null){
    Console.Write(contactinfo.Phone);

}

使用
first或Default
时,
tblcustomecontactinformationphone
的“默认”值为
null
。如果对该表的查询没有找到任何内容(意味着没有符合2的
PhoneTypeID
的记录),您将得到
null
,并且在代码中您试图得到
null
Phone
。您的
在此无效。

很可能没有任何条目符合您的
PhoneTypeID==2
标准,因此
.FirstOrDefault()
返回
null
。试图访问的
.Phone
属性会引发空引用异常

其他答案表明,您可以对
.FirstOrDefault()
的结果执行空检查,但您可以使用Entity Framework的另一个技巧,仅查询
Phone
属性,并避免选择超出需要的数据:

altPhone = cust.tblCustomerContactInformation1.tblCustomerContactInformationPhone
    .Where(p => p.PhoneTypeID == 2)
    .Select(p => p.Phone)
    .FirstOrDefault() ?? "";

这是因为SQL Server在遇到空值时不会引发异常,而是传播空值。

请显示所有相关代码,说明如何定义cust或您正在声明的所有其他对象。。您不能声明对象的实例,例如
MyObject MyObject
并期望在不使用对象的情况下获得对其任何字段或属性的访问权和/或分配值。。在google上搜索如何实例化对象引用的示例
。如果没有返回行,FirstOrDefault()
将返回
null
。您无法访问
null.Phone
。可能的重复项我已经尝试了几种方法来解决这个问题。包括
??
并显式检查
null
(如果(…!=null))。@DaveJohnson:是的,问题是在尝试从null值访问属性之后使用了null合并运算符。很高兴你把事情弄清楚了,谢谢。这两种方法都有效,尽管你的更新版本基本上就是我想做的。对Linq了解不多使我很难做这些简单的事情。