Linq ThenBy Possibly Null
c#, linq
Solution
This should return null Pets before non-null Pets.
return this.People
.OrderBy(x => x.Car.Name)
.ThenBy(x => x.Pet != null ? x.Pet.Name : "");
Problem
I'm trying to sort a view model binding on multiple properties. The problem is that the second property might be null, and I get a null reference exception. ``` return this.People .OrderBy(x => x.Car.Name) .ThenBy(x => x.Pet.Name); ``` What if Pet is null? How do I still do a ThenBy sort by Pet.Name?