Given the code:
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
}
After the safe cast (i as EmployeeDto)
may I receive a NullReferenceException. How can I ensure and safety of the code and not overload him with a lot of null checks?
Overview of solutions:
I did some tests to assert if the solutions are working. Both are working well and bring the same result, you can check HERE. After that I did some performance tests with OfTypeSolution and letSolution.
As OfType solution have better times in average, this will be the answer!