下面是一段代码:
var test = myListOfItems.Select(x => MyMethodReturningObject(x.SubItems) != null ? theValue : x.SubItems[0])
我希望如果MyMethodReturningObject返回一个对象,我将返回值赋给value。
有点像这样:
var test = myListOfItems.Select(x => MyFunctionReturningObject(x.SubItems) != null ? MyMethodReturningObject(x.SubItems): x.SubItems[0])
但是不调用该方法两次。
可以通过Linq实现吗?
谢谢!
var test = myListOfItems.Select(x => MyFunctionReturningObject(x.SubItems) ?? x.SubItems[0])