2

所以我有这些部分课程。有关 ParseFCIE 的部分实现和我的问题,请参见下文。

class CiscoSwitch
{
    Dictionary<int, CiscoVSAN> VSANList = new Dictionary<int, CiscoVSAN>();
    public void ParseFCIE(string block) 
    {}
}

class CiscoVSAN
{
     Dictionary<string, CiscoSwitch> MemberSwitches = new Dictionary<string, CiscoSwitch> ();
} 

ParseFCIE 的一部分是检查输入数据中的传入交换机是否已经在任何 CiscoVSAN 对象的 SwitchMembers 字典中,如果没有,则添加它。我有 2 个字典语句。第一条语句有效,第二条编译器说它无法确定谓词的类型,我不知道为什么。我更喜欢第二个第二个陈述,因为它只是一个步骤。我搜索开关的第一种方法是检查搜索结果中的空值。

ParseFCIE(string block)
{  
     string DID = string.Empty;
     //partial implementation
     // 'this' is a CiscoSwitch object
     //this works
     var vsans= this.VSANList.SelectMany(v => v.Value.MemberSwitches.
                          Where(d => d.Value.switchName == this.switchName)); 
     // assume DID now has a value;
     // this line the compiler says the type arguments cannot be inferred from usage
     if (this.VSANList.SelectMany(v => v.Value.MemberSwitches.ContainsKey(DID))) 
     {}
}
4

2 回答 2

2
if (this.VSANList.SelectMany(v => v.Value.MemberSwitches).Any(x => x.ContainsKey(DID))) {
}
于 2012-09-12T03:06:05.780 回答
0

v.Value.MemberSwitches.ContainsKey(DID)返回 a boolean,指示该嵌套字典是否包含该键。
SelectMany()需要一个返回集合的委托以使其展平。

你需要打电话Any()

于 2012-09-12T03:06:12.677 回答