Lambda “if” statement?

前端 未结 5 642
我寻月下人不归
我寻月下人不归 2021-02-04 13:36

I have 2 objects, both of which I want to convert to dictionarys. I use toDictionary<>().

The lambda expression for one object to get the key is (i => i.name). For th

5条回答
  •  面向向阳花
    2021-02-04 13:48

    Yes, the conditional operator ("ternary operator") does what you want:

    (i => i.name != null ? i.name : i.inner.name)
    

    Assuming, of course, that you can detect the "existence" of the name by checking for null.

    Edit: In that case, Kirschstein's answer is better, of course.

提交回复
热议问题