';对象引用未设置为对象的实例';来自匿名数组的错误
本文关键字:数组 错误 对象 对象引用 设置 实例 | 更新日期: 2023-09-27 18:30:14
InquiryOrder模型
public class InquiryOrder
{
[Key]
public int InquiryOrderId { get; set; }
public string InquiryOrderName { get; set; }
[ForeignKey("ProductType")]
public int? ProductTypeId { get; set; }
public virtual ProductType ProductType { get; set; }
}
产品类型型号
public class ProductType
{
[Key]
public int ProductId { get; set; }
[StringLength(100, ErrorMessage = "Must be less than 100 charcters", MinimumLength = 1)]
[Required(ErrorMessage = "Required")]
public string ProductTypeName { get; set; }
public virtual List<InquiryOrder> inquiryOrders { get; set; }
}
查询订单控制器
var displayedInquiryOrders = filteredInquiryOrders.Skip(iDisplayStart).Take(iDisplayLength);
var result = from c in displayedInquiryOrders .AsEnumerable()
select new[] {
Convert.ToString(c.InquiryOrderId),
c.InquiryOrderName,
c.ProductType.ProductTypeName,
};
这里从c.ProductType.ProductTypeName
,我得到这个错误Object reference not set to an instance of an object
。请帮我解决这个问题。谢谢
此处为
new[] {
Convert.ToString(c.InquiryOrderId),
c.InquiryOrderName,
c.ProductType.ProductTypeName,
};
任一:
c
为空c.ProductType
为空
您需要添加一些NullReference控件。请试试这个:
select new[]{
Convert.ToString(c?.InquiryOrderId),
c?.InquiryOrderName,
c?.ProductType?.ProductTypeName,
};