2012-09-12 50 views
2

所以我有这些部分类。请参阅下面的部分实现ParseFCIE和我的问题。 ParseFCIE的词典和SelectMany()

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>(); 
} 

部分是检查在输入数据的传入开关是否已在任何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))) 
    {} 
} 

回答

2
if (this.VSANList.SelectMany(v => v.Value.MemberSwitches).Any(x => x.ContainsKey(DID))) { 
} 
0

v.Value.MemberSwitches.ContainsKey(DID)返回一个boolean,指示嵌套字典中是否包含键。
SelectMany()需要一个委托,它返回一个集合,使其变平。您需要致电Any()

+0

感谢您的解释。这就说得通了。 –