2

I am writing a C# application and I have troble parsing an object that is inside of list of list. Right now, I have something like,

List<List<MyObj>> list;

and I am just trying to get the list of MyObj. And, it looks like the outer list is generic. How do I just get the inner list (List)?

Thank you for your help.

1
  • use code markup to let us see the code please Commented Sep 2, 2010 at 19:56

5 Answers 5

6

If you just want a specific one of the inner List<MyObj> then use the indexer

List<MyObj> local = list[0];

If you want all of the inner lists viewed as a single List<MyObj> with the inner ones just being combined together then use SelectMany

List<MyObj> local = list.SelectMany(x => x).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

List inner = outerlist[indexOfListYouWant]

A list inside a list can be accessed just like any other object in that list.

Comments

1

You need only iterate over the outer list like so:

foreach(List<MyObj> innerList in list)
{
    // do stuff to innerList
}

Comments

0

Which is the "inner lists" do you want?

 List<List<MyObj>> list;
 List<MyObj> MyObjList = list[0];

Comments

0

To access one (here, the first) of the many inner lists:

List<MyObj> inner = list[0];

To turn this "jagged" 2-dimensional list into a single one-dimensional list:

List<MyObj> oneList = list.Aggregate(new List<MyObj>(), (inner, result) => result.Concat(inner)).ToList();

Comments

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.