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?
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 : "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With