Lambda “if” statement?

前端 未结 5 615
我寻月下人不归
我寻月下人不归 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:52

    something along the lines of

    collection1.ForEach(i => myDictionary.Add((i.name.length == 0 ? i.inner.name : i.name),value);
    

    (untested) should do the trick if i.name is not null (an empty string), or

    collection1.ForEach(i => myDictionary.Add((i.name ?? i.inner.name),value);
    

    (also untested)

提交回复
热议问题