我有以下查询:
List<Models.PricingFormula> formulasInCat = new List<Models.PricingFormula>();
productsInCat = (from x in Model.PPPVMs
where x.CategoryId == category.ProductCategoryId select x).ToList();
查询没有返回任何记录,我得到的错误是:
值不能为空。
处理这个问题的正确方法是什么?
我有以下查询:
List<Models.PricingFormula> formulasInCat = new List<Models.PricingFormula>();
productsInCat = (from x in Model.PPPVMs
where x.CategoryId == category.ProductCategoryId select x).ToList();
查询没有返回任何记录,我得到的错误是:
值不能为空。
处理这个问题的正确方法是什么?
您可以DefaultIfEmpty()在调用该ToList()方法之前使用。
如果Modelorcategory为空,就会有NullReferenceException. Value cannot be null是 的消息ArgumentNullException,这意味着最有可能的 PPPVM 为空。
List<Models.PricingFormula> productsInCat;
if (Model.PPPVMs == null)
productsInCat = new List<Models.PricingFormula>();
else
productsInCat = (from x in Model.PPPVMs
where x.CategoryId == category.ProductCategoryId select x).ToList();