tags:

views:

83

answers:

2

My friends were having discussion on nested lists of objects in LINQ and when I asked what does that mean, they laughed :(

Can anyone here tell what is nested list. is it same like list inside list? Thank you to all who help me

Is this NestedList?

public class X
{
public Y[] y { get; set; }   
}

public class Y
{
int id { get; set; }
}
+4  A: 

Yes, it sounds like a list inside a list, for example:

List<List<int>>

It wasn't nice to laugh at you though - it may indicate your code is better organized into logical classes. Such constructs, in general, should be avoided - you probably wouldn't want to expose or use an interface with too complicated types, but it is common to use such lists locally, specially when using LINQ.

A simple example where this can be useful would be:

string[] directories = Directory.GetDirectories("c:\\");
var files = directories.Select(Directory.GetFiles).ToList();

Here, files is a List holding an array of strings: List<string[]>, which is quite similar. It is very likely your friends weren't talking about lists in particular, it is just as common you work with arrays, dictionaries, or IEnumerables.

The posted code does not have nested, or two dimensional lists. X has a array of Y, and that's it. If you has a list of Xs, however, that would have nested lists:

IEnumerable<X> xfiles = GetXFiles();
foreach(X file in xfiles)
{
    foreach(Y section in file)
    {
        //...
    }
}

It comes down to a simple point: an object can hold other objects, with any complexity.

Kobi
+1 for the last part, its usually better and more readable to use classes to organize your information rather than nested generic lists
Lerxst
I wanted an example of a nested list of objects..what does it look like and why do you say it is to be avoided
Jasl
@Jasl - added an example and some more details. Remember that "List" and "Object" are general terms, an Enumerable can be seen as a List, and in C# "Object" can describe almost everything you're working with - even functions. Don't look specifically for `List<List<object>>` - that is poor use of generics.
Kobi
Nested list looks like this f.e. : List<List<object>>It should be avoided because the access on the inner object gets harder and the code is not that friendly for other developers
KroaX
I was searching the net and saw this public class X { public Y[] abc { get; set; } } Is this nested list?
Jasl
Kobi i have update my question..can you confirm if that is nested list..thank you for help
Jasl
So the example i posted will be more of a Parent-Child relationship?
Jasl
Yes, if you want to demonstrate nested lists, you need to one extra step of complexity.
Kobi
Ok thanks for telling me that what i posted was parent-child. i am now clear with what is a nested list vs parent-child :)
Jasl
+1  A: 

they may mean a List of Lists, such as

List<List<String>> listoflists = new List<List<String>>();
listoflists.Add(new List<String>(new string[]{"One","Two"}));
listoflists.Add(new List<String>(new string[]{"A","B","C"}));
Lerxst
You're missing `()` at the constructor. Also, depending on the version of C#, you may simplify it to `listoflists.Add(new List<String> {"One","Two"});`.
Kobi
I wanted an example of a nested list of objects..what does it look like
Jasl
@Jasl, this is what it looks like :-) Just replace "String" with "Object" and there you go.The nested list is just a list of collections of objects, as outlined in my example
Lerxst
@Kobi, thanks, it was late ;)
Lerxst