2016-03-15 118 views
1

我正在使用Open XML &我有一个IDictionary<String, List<OpenXmlCompositeElement>>结构。我想与结构的List部分一起工作,但this.map.Values尝试将其包装在ICollection中。我如何从我的结构中获取列表部分?IDictionary <String,List <OpenXmlCompositeElement >> - 获取List <OpenXmlCompositeElement>?

public List<OpenXmlCompositeElement> MapData() 
    { 
     //this does not work 
     return this.map.Values; 
    } 
+0

哪一个?可能会有不止一个。 –

+1

只需使用该键即可访问您想要的那个。或通过字典循环? 'List list = dictionary [“key”];' – iswinky

+0

是的,我终于知道有多个列表:)。这终于给了我所寻找的东西:test = dynamicContent.MapData()。Any(l => l.Any(i => i.Descendants()。OfType ().Count()> 0));我需要确定我的Open XML元素列表是否有任何图像。 –

回答

2

由于它是一本字典,它期望您告诉你想要哪个键值。

所以这将是你所需要的代码,其中yourKey是要检索的关键:

public List<OpenXmlCompositeElement> MapData() 
{ 
    return this.map["yourKey"]; 
} 

如果你有钥匙没有兴趣,和字典只是一个字典,因为串行说所以,你可以得到的第一个项目,例如像这样:

public List<OpenXmlCompositeElement> MapData() 
{ 
    return this.map.Values.First(); 
} 
+0

this.map.Values.First()给了我想找的东西。谢谢。 –

0

无论是环翻翻字典,你可以和使用您希望值,或直接使用的密钥(在这种情况下,它是一个字符串访问列表)

IDictionary<String, List<OpenXmlCompositeElement>> myDictionary; 

List<OpenXmlCompositeElement> myList = myDictionary["myKey"]; 

其中myKey在字典中有效。

或者可以遍历

foreach (var item in myDictionary) 
{ 
    var key = item.Key; 
    var value = item.Value 

    // You could then use `key` if you are unsure of what 
    // items are in the dictionary 
} 
0

假设这是你的字典...

IDictionary<string, List<OpenXmlCompositeElement>> items = ...; 

获得通过密钥的特定列表...

List<OpenXmlCompositeElement> list = items["key"]; 

获得第一列表中的字典...

List<OpenXmlCompositeElement> list = items.Values.First(); 

串接在字典到一个列表的所有列表...

List<OpenXmlCompositeElement> list = items.SelectMany(o => o).ToList(); 
0
foreach(KeyValuePair<string, List<OpenXmlCompositeElement>> kvp in IDictionary) 
    { 
    string key = kvp.key 
    List<OpenXmlCompositeElement> list = kvp.Value; 
     foreach(OpenXmlCompositeElement o in list) 
     { 
     Do anything you need to your List here 
     } 
    } 

我与字典的工作一样,所以这里是我目前正与一个真实的例子:

foreach(KeyValuePair<string, List<DataRecords>> kvp in vSummaryResults) 
     { 
      string sKey = kvp.Key; 
      List<DataRecords> list = kvp.Value; 
      string[] vArr = sKey.Split(','); 

      int iTotalTradedQuant = 0; 
      double dAvgPrice = 0; 
      double dSumQuantPrice = 0; 
      double dQuantPrice = 0; 
      double dNumClose = 0; 

      foreach (DataRecords rec in list) 
      { 
       if(vSummaryResults.ContainsKey(sKey)) 
       { 
        iTotalTradedQuant += rec.iQuantity; 
        dQuantPrice = rec.iQuantity * rec.dInputTradePrice; 
        dSumQuantPrice += dQuantPrice; 
        dAvgPrice = dSumQuantPrice/iTotalTradedQuant; 
        dNumClose = rec.dNumericClosingPrice; 

       } 

       else 
       { 
        vSummaryResults.Add(sKey, list); 
        //dNumClose = rec.dNumericClosingPrice; 
       } 
相关问题