如果找不到匹配项,我想检查下面的代码,它不应该抛出对象引用null错误。
itm.lstCustomziation.AddRange(
(from xx in db.vw_DressingCustomization
where xx.CatID == itm.HeaderName.Value && xx.ProductID == itm.ProductID
select new itmCustomization()
{
catId = (int)xx.CatID,
custType = customizationType.Dressing,
isCustomziationDisplay = xx.IsDefaultDisplay,
isFixLimit = (bool)xx.isDefaultLimit,
maxLimit = (short)xx.DefaultFreeCount,
itmName = xx.Description,
isItemDefault = xx.IsDefaultDisplay,
price = (double)xx.MainPrice,
proId = (int)xx.ProductID
}).ToList<itmCustomization>());
答案 0 :(得分:0)
你应该把它分成多个步骤。首先执行查询并获取结果。然后你应该检查null / count并根据该检查调用add range方法。它也会更具可读性。
答案 1 :(得分:0)
在访问该值之前,请检查您的可空itm.HeaderName
是否确实有值:
where itm.HeaderName.HasValue && // check if value exists
xx.CatID == itm.HeaderName.Value && // otherwise here is the problem
xx.ProductID == itm.ProductID