我有一个描述存储的各种手机的类。有时Importance
属性可能为null。以下是该类:
public class PhoneTypeListInfo
{
public string AccountNum { get; set; }
public int PhoneType { get; set; }
public string PhoneNum { get; set; }
public int Importance { get; set; }
}
我定义了一个函数,如果电话号码和账户号码匹配给定的一组值,它将返回一个PhoneTypeListInfo
。
protected PhoneTypeListInfo RetrievePhoneType(string info, string acctNumber)
{
PhoneTypeListInfo type = xPhoneTypeList.Where(p => p.PhoneNum == info && p.AccountNum == acctNumber).FirstOrDefault();
return type;
}
这一切都很好。我的问题在于下面的linq查询。
List<AlertBasedPhones> xAccountPhones = new List<AlertBasedPhones>();
xAccountPhones = (from x in xAccountStuff
where x.Media == "Phone"
let p = RetrievePhoneType(x.Info, acct.AccountNumber)
let xyz = x.Importance = (p.Importance as int?).HasValue ? p.Importance : 0
orderby p.Importance descending
select x).ToList();
我上面所做的是尝试使用一个不同构成的类,除了从PhoneTypeListInfo获取'Importance'属性以外。
我的问题最终是,我需要做什么才能允许
p.Importance
为空,并将它设置为0,如果它为空,则使x.Importance
也为0。
p.Importannce
,而是p
本身。 - Scott ChamberlainRetrievePhoneType
是否返回了null
以及p本身。 - Mong Zhu