LAMBDA"若"声明? [英] Lambda "if" statement?

查看:126
本文介绍了LAMBDA"若"声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象,这两个我要转换为dictionarys。我用toDictionary<>()

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

一个对象来获得密钥的lambda表达式为(i => i.name)。对于其他的,它是(ⅰ=> i.inner.name)。在第二个,i.name不存在。如果i.name不i.inner.name始终存在。

The lambda expression for one object to get the key is (i => i.name). For the other, it's (i => i.inner.name). In the second one, i.name doesn't exist. i.inner.name ALWAYS exists if i.name doesn't.

有一个lambda表达式我可以用这两个结合起来?基本上为读取:

Is there a lambda expression I can use to combine these two? Basically to read as:

如果i.name存在,那么设置id来i.name,否则设置id来i.inner.name。

"if i.name exists then set id to i.name, else set id to i.inner.name".

非常感谢。

更新

当我说不存在,我指的是对象不实际拥有的属性,而不是属性只是空。

When I say "don't exist", I mean the objects don't actually have the properties, not that the properties are just null.

推荐答案

你为什么不给每个对象的 ToDictionary 方法他们自己,因为他们显然有自己的行为,在这种情况下。

Why don't you give each object a ToDictionary method of their own, as they obviously have their own behaviours in this case.

如果您不能添加到对象,因为你不拥有它们,你可以随时为他们写的扩展方法。

If you can't add to the objects, because you don't own them, you can always write extension methods for them.

任何原因试图强迫他们喂成一个共同的功能?

Any reason your trying to force feed them into one "common" function?

这篇关于LAMBDA&QUOT;若&QUOT;声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆