Turp
Turp

Reputation: 708

Accessing object[] with different Type objects

I am calling a third party service and in the Response they're returning an object[] called Items. This object array has a few different objects with different types inside its array.

Here's what the Items look like in the CS file:

[System.Xml.Serialization.XmlElementAttribute("REPORT", typeof(REPORT))]
[System.Xml.Serialization.XmlElementAttribute("_PRODUCT", typeof(_PRODUCT))]
[System.Xml.Serialization.XmlElementAttribute("_PROPERTY_INFORMATION", typeof(_PROPERTY_INFORMATION))]
public object[] Items
{
    get
    {
        return this.itemsField;
    }
    set
    {
        this.itemsField = value;
    }
}

I need to access the _PROPERTY_INFORMATION object inside the Items array. However, I am not sure what the best way is to approach such a task.

Here is what I currently have:

var items = RESPONSE.RESPONSE_DATA[0].PROPERTY_INFORMATION_RESPONSE.Items;
foreach (_PROPERTY_INFORMATION info in items)
{
    parsedStreetAddress = info.PROPERTY._PARSED_STREET_ADDRESS;
}

Is there a better way with less lines of code to accomplish the same thing? I'm just getting each _PROPERTY_INFORMATION that's inside the Items array.

Upvotes: 2

Views: 75

Answers (1)

Alex H
Alex H

Reputation: 1503

How about using LINQ

_PROPERTY_INFORMATION result = RESPONSE.RESPONSE_DATA[0].PROPERTY_INFORMATION_RESPONSE.Items
    .First(x => x is _PROPERTY_INFORMATION);

Upvotes: 1

Related Questions