开发者

List inside a list in linq - what is it?

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; }
}


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.


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"}));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜