3

I've looked at similar questions but nothing quite fits. I have an object which happens to contain a List. I'd like to get it into something I can enumerate.

For example:

object listObject;          // contains a List<Something>
List<object> list;

list = listObject as List<object>;   // list contains null after

foreach ( object o in list )
{
    // do stuff
}

The conversion from object to List<object> is the problem.

EDIT:

What I finished with:

object listObject;          // contains a List<Something>
List<object> list;

IEnumerable enumerable = listObject as IEnumerable;

if ( enumerable != null )
{
    list = enumerable.Cast<object>().ToList();

    foreach ( object o in list )
    {
         // do stuff
    }
}

If you watch this demo video of Altova XMLSpy and advance to 2:35 you will see how clicking on one of the toolbar buttons (which appears to be labeled "Pretty-print") will format the results of your XQuery as nicely indented XML.

4

1 に答える 1

7

Try This:

list = (listObject as IEnumerable).Cast<object>().ToList()
于 2013-03-12T03:35:30.087 に答える