【发布时间】:2013-04-23 08:11:53
【问题描述】:
我在 c# 4.0 中有以下代码。
//Dictionary object with Key as string and Value as List of Component type object
Dictionary<String, List<Component>> dic = new Dictionary<String, List<Component>>();
//Here I am trying to do the loping for List<Component>
foreach (List<Component> lstComp in dic.Values.ToList())
{
// Below I am trying to get first component from the lstComp object.
// Can we achieve same thing using LINQ?
// Which one will give more performance as well as good object handling?
Component depCountry = lstComp[0].ComponentValue("Dep");
}
【问题讨论】:
-
如何检查这个条件 Component depCountry = lstComp[0].ComponentValue("Dep");获取第一很容易如何从 lstComp[0] 组件获取 Component 对象,因此组件具有组件
-
也将
ToList()放在Values上,这不是必需的,需要创建一个额外的对象并枚举整个值集合。 -
鲍勃,你有什么建议而不是使用 ToList() 因为我需要整个 List
对象来进行循环