我应用了以下代码:
var country = from cnty in this.GetAll<CompanyDefinition>().ToList()
where cnty.IsImported = true
select new {
CompanyDefinitionID = cnty.CompanyDefinitionID
, CompanyDefinitionName = cnty.Company.CompanyName + "(" + cnty.Country.CountryCode + "," + cnty.NaicsCode.NaicsCode1 + ")"
};
我收到对象引用错误。它指向“选择新”。正确的做法是什么?
最佳答案
问题是 Company
,Country
或 NaicsCode
是 null
,您需要在尝试之前检查一下访问他们的属性。例如,您可以将查询重写为:
var country = from cnty in this.GetAll<CompanyDefinition>()
where cnty.IsImported && cnty.Company != null && cnty.Country != null && cnty.NaicsCode != null
select new {
...
}
关于c# - LINQ 中的对象引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12111049/