Search code examples
c#openxml

IDictionary<String, List<OpenXmlCompositeElement>> - get the List<OpenXmlCompositeElement>?


I'm working with Open XML & I have a IDictionary<String, List<OpenXmlCompositeElement>> structure. I want to work with the List part of the structure but this.map.Values tries to wrap it in an ICollection. How can I get the List part from my structure?

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

Solution

  • Since it is a dictionary, it expects you to tell from which key you want the value.

    So this would be the code you need, where yourKey is the key you want to retrieve:

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

    If you have no interest in the key, and the dictionary is just a dictionary because the serializer says so, you could get the first item for example like this:

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