How to get first object out from List<Object> using Linq

前端 未结 10 1334
自闭症患者
自闭症患者 2020-12-29 00:51

I have below code in c# 4.0.

//Dictionary object with Key as string and Value as List of Component type object
Dictionary>         


        
10条回答
  •  再見小時候
    2020-12-29 01:50

    I would to it like this:

    //Dictionary object with Key as string and Value as List of Component type object
    Dictionary> dic = new Dictionary>();
    
    //from each element of the dictionary select first component if any
    IEnumerable components = dic.Where(kvp => kvp.Value.Any()).Select(kvp => (kvp.Value.First() as Component).ComponentValue("Dep"));
    

    but only if it is sure that list contains only objects of Component class or children

提交回复
热议问题