2

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.

2
  • Are you only expecting there to be one PROPERTY_INFORMATION instance in items? Or do you just want the last one? Commented Jul 13, 2015 at 13:27
  • Check the type of each item - if it's the type you're looking for, then cast and access the property. Commented Jul 13, 2015 at 13:28

1 Answer 1

1

How about using LINQ

_PROPERTY_INFORMATION result = RESPONSE.RESPONSE_DATA[0].PROPERTY_INFORMATION_RESPONSE.Items
    .First(x => x is _PROPERTY_INFORMATION);
Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me! Exactly what I was wanting. I didn't think about using LINQ for this. I greatly appreciate your answer.
@Turp Its always nice to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.