在Linq中处理空值的最佳方法是什么?LINQ to SQL处理空值
我有这样的代码从数据库中检索客户的联系,但如果不存在的联络方式它会创建一个新的实例
void SetProperty(int _CustomerID)
{
Contacts_GetResult Contact;
if (Global.VariableStore._Contact == null)
{
Contact = Cd.Contacts_Get(_CustomerID).SingleOrDefault();
if (Contact == null)
Contact = new Contacts_GetResult();
Global.VariableStore._Contact = Contact;
}
else
{
Contact = Global.VariableStore._Contact;
}
if (Contact != null)
{
HomeNumber.Value = Contact.HomeNumber.ToString();
MobileNumber.Value = Contact.MobileNumber.ToString();
WorkNumber.Value = Contact.WorkNumber.ToString();
EmailAddress.Value = Contact.EmailAddress.ToString();
}
当创建新的联系人的所有值都为空,这使得下面的代码失败,因为该值为null
HomeNumber.Value = Contact.HomeNumber.ToString();
我目前使用的:
if (Contact.HomeNumber != null)
HomeNumber.Value = Contact.HomeNumber.ToString();
有没有更简单的方法?