2016-03-15 117 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; 
       } 
相關問題