给定代码:
from i in this.GridViewFoo.SelectedItems
select new EmployeeEntity
{
EmployeeID = (i as EmployeeDto).EmployeeID,
Email = this.GetAllEmail((i as EmployeeDto).Email, (i as EmployeeDto).SecondaryEmails),
EmployeeNumber = (i as EmployeeDto).EmployeeNumber,
FirstName = (i as EmployeeDto).FirstName,
LastName = (i as EmployeeDto).LastName
}
在安全转换之后,(i as EmployeeDto)
我可能会收到 NullReferenceException。我怎样才能确保代码的安全性,而不是用大量的空检查使他超负荷?
解决方案概述:
我做了一些测试来断言解决方案是否有效。两者都运行良好并带来相同的结果,您可以在此处查看。之后我用OfTypeSolution和letSolution做了一些性能测试。
由于 OfType 解决方案的平均时间更好,这就是答案!