I am new to Linq and Entity Framework, and am receiving a null reference exception on this query:
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;
)
There is a single row in tblCustomerContactInformationPhone. The Where clause should remove it, and I should end up getting an empty string. However, instead I receive:
Object reference not set to an instance of an object.
What am I doing wrong, and how do I do this correctly so that an empty result set is properly converted to an empty string?
The linked question is not helpful, as this is specific to the use of Linq, which that question does not cover. @evanmcdonnal's answer was quite helpful, and solved my problem.