2

I have an array that contains 2 elements and each element can contain one or more object with two properties: Id -> (string) and Names -> (List<string>).

You'll probably understand better by taking a look at the following image: enter image description here

How can I loop through the elements of the second object and get the Id and Names properties for each of them?

This is how I've tried to implement it, but doesn't work:

foreach (var elem in classArray[1].GetType().GetProperties())
{
    var id = elem.Id;
    var name = elem.Names;
}

This is the Classification class:

public class Classification
{
    public string Id { get; set; }
    public List<string> Names { get; set; }
}

This is my code:

    List<object> classCollection = new List<object>();
    dynamic response = JsonConvert.DeserializeObject(result);
    for (int i = 0; i < response.Count; i++)
    {
        var classObj = response[i].element_classifications;
        var className = new List<string>();
        List<Classification> classList = new List<Classification>();

        for (int k = 0; k < classObj.Count; k++)
        {
            var classification = classObj[k].classifications;
            for (int j = 0; j < classification.Count; j++)
            {
                className.Add(classification[j].name.Value);
            }
            classList.Add(new Classification { Id = classObj[k].id.Value, Names = className });
        }
        classCollection.Add(classList);
    }
    var classArray = classCollection.ToArray();
4
  • 1
    Do you mean they can be any object, or they are always of type ExcelSDRAddIn.UserControlSDR.Classification? Commented Apr 6, 2017 at 19:51
  • They are always of that type Commented Apr 6, 2017 at 19:52
  • 1
    Why are you using object[] then if the items are all of the same type? Using a strongly-typed collection will make life a lot easier. Commented Apr 6, 2017 at 20:07
  • I updated my question with my code ... It would help me a lot if you can tell me how to use a strongly-typed collection in my case. Thank you! Commented Apr 6, 2017 at 20:11

3 Answers 3

2

You can cast the objects to the appropriate class:

foreach (var elem in ((IEnumerable)classArray[1]).Cast<ExcelSDRAddIn.UserControlSDR.Classification>())
{
    var id = elem.Id;
    var name = elem.Names;
}
Sign up to request clarification or add additional context in comments.

5 Comments

This returns me the following errors: cannot convert from 'object' to 'System.Linq.IQueryable' and 'object' does not contain a definition for 'Cast' and the best extension method overload 'System.Linq.Queryable.Cast<TResult>(System.Linq.IQueryable)' has some invalid arguments
@Valip - you may need to also cast classArray[1], like so: ((IEnumerable)classArray[1]).Cast<ExcelSDRAddIn.UserControlSDR.Classification>()
It says that Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires 1 type arguments
It works if I initialize classCollection as follows: List<ExcelSDRAddIn.UserControlSDR.Classification[]> classCollection = new List<ExcelSDRAddIn.UserControlSDR.Classification[]>();
You would cast it to System.Collections.IEnumerable, not System.Collections.Generic.IEnumerable<T>. But you figured it out so hurray!
0

If the object is always the same

foreach(ExcelSDRAddIn.UserControlSDR.Classification variableName in classArray[i])

Or you can use dynamic or var

dynamic v = classArray[i].Id;
dynamic name = classArray[i].Names;

Comments

0

From your image, it looks like this would work:

foreach(var item in classArray[1])
{
    var typedItem = (ExcelSDRAddIn.UserControlSDR.Classification) item;

    Console.WriteLine($"Id {typedItem.Id} has names:");

    for (int i = 0; i < typedItem.Names.Count; i++)
    {
        Console.WriteLine($" - {typedItem.Names[i]}");
    }
}

4 Comments

I've tried this too, but it returns foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'
sorry, I missed that your array is of type object I updated my answer
@Valip is there some reason your array is not strongly typed to start with? Why is it not defined as: ExcelSDRAddIn.UserControlSDR.Classification[] classArray;
There is no reason my array is not strongly typed ... I just didn't know how to make it strongly typed. I updated the question with my code.

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.