Serge P
Serge P

Reputation: 1611

How to iterate through a list within a generic list

I'm trying to serialize each object per each list collection inside a generic list of Objects

As an example I can iterate through each list object alist inside the generic list temp_list, but since I don't know what the v object is until run-time I can't reference its list:

List<AObj> alist = new List<AObj>();
List<BObj> blist = new List<BObj>();

// .. objects initialized here, plugged into respective lists, etc.

List<Object> temp_list = new List<Object>();
temp_list.Add(alist);
temp_list.Add(blist);

foreach (var v in temp_list)
{
    // Here I want to iterate through the list in each v
    /* 
    foreach (var w in v) <-- This obviously doesn't work because the compiler
                             doesn't know what v is until run-time
    */
}

Where alist and blist have just basic objects:

class AObj
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

class BObj
{
    public string Property3 { get; set; }
    public string Property4 { get; set; }    
}

Is there a way I can have a double foreach that would let me iterate through each object within a list that is within a generic list.

Since the compiler doesn't know v until run-time it's pretty much stuck in limbo right.

Upvotes: 1

Views: 5857

Answers (3)

gotopie
gotopie

Reputation: 2654

Using linq...

List<AObj> alist = new List<AObj>();
List<BObj> blist = new List<BObj>();

// .. objects initialized here, plugged into respective lists, etc.

List<object> temp_list = new List<object>();

temp_list.Add(alist);
temp_list.Add(blist);

var allObjects = temp_list
    .Select(l => l as IEnumerable<object>)  // try to cast to IEnumerable<object>
    .Where(l => l != null)                  // filter failed casts
    .SelectMany(l => l);                    // transform the list of lists into a single sequence of objects

foreach (var o in allObjects)
{
    // enumerates objects in alist and blist.
}

Upvotes: 0

pushpraj
pushpraj

Reputation: 13669

if you are sure that it is iteratable then casting will work

eg

foreach (var v in temp_list)
{
    // Here I want to iterate through the list in each v
    foreach (var w in v as IEnumerable) //casting to IEnumerable will let you iterate
    {
       //your logic
    }
}

alternatively you may change the outer list type to avoid casting

from

List<Object> temp_list = new List<Object>();

to

List<IEnumerable> temp_list = new List<IEnumerable>();

if it is not possible then perhaps adding a check before iterating should make the code safe

foreach (var v in temp_list)
{
    // Here I want to iterate through the list in each v
    //casting to IEnumerable will let you iterate
    IEnumerable list = v as IEnumerable;
    //check if successful
    if(list != null)
    {
        foreach (var w in list)
        {
           //your logic
        }
    }
    else
    {
        //else part if needed
    }
}

Upvotes: 3

Baldrick
Baldrick

Reputation: 11840

You need declare your temp list as a list of IEnumerable<object>, instead of object. This is done as follows:

var alist = new List<AObj>();
var blist = new List<BObj>();

// .. objects initialized here, plugged into respective lists, etc.

var temp_list = new List<IEnumerable<object>>();

temp_list.Add(alist);
temp_list.Add(blist);

foreach (var v in temp_list)
{
    foreach (var x in v)
    {

    }
}

Upvotes: 0

Related Questions