简体   繁体   中英

List Orderby property from nullable object

I have a collection of objectAs in a list. There is an objectB within the objectA. I want to sort the list by objectB.Prop1 but i keep getting a null reference when it sorts through the list.
This is what I have in my code:

data.OrderBy(c =>c.obj.Name);

I know I'm getting the null reference because some of the items have obj null. I have thought of separating the list into two: one with null obj, and one that has an object for obj then sort, then concat afterwards. But I am wondering if there is a better way to do that.

这会有所帮助吗?

data.OrderBy(c => c.obj != null? c.obj.Name : "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM