0

Now I'm having a problem of populating some sub lists. I have a main list called "list" of type FileInfo with thousands of files. What I want to do is to split this main list into several sub lists, each of which contains a pack of files with the same name. I did sth like this:

var File = list;
string[] foldername = Names.Distinct().ToArray();
List<TreeNode> N1 = new List<TreeNode>();
List<FileInfo>[] sublist = new List<FileInfo>[foldername.Length];

for (int j = 0; j < foldername.Length; j++)
{
    N1.Add(clicked.Nodes.Add(foldername[j]));
    foreach (FileInfo file in File)
    {
        if (file.Name== N1[j].Name)
                sublist[j].Add(file);
    }
}

For example the foldername array has 7 elements, so I want to have 7 sublists. Files included in each sublist have the same name with the tree node N1[j]. The result, however, is that the

sublist[j].Add(file)

isn't excuted at all, and every sublist is null. I have no idea what's wrong. Anybody got any idea? Thank you

Tzah Mama
  • 1,547
  • 1
  • 13
  • 25
jcraffael
  • 81
  • 3
  • 12
  • can I suggest you do `Names.GroupBy(x => x).ToList()`? (note this will still be strings and not `FileInfo`) – Sayse Jun 11 '14 at 08:42
  • @jcraffael Are you sure you `File` names are matching the `Names` names? I think the `if (file.Name== N1[j].Name)` is never evaluated to `true`. – Maarten Jun 11 '14 at 08:48
  • @jcraffael What is stored in N1? If they contain the filenames with the pathnames included, then the strings won't be the same. And have you tried file.Name.Equals(N1[j].Name) instead of == ? – Dennis_E Jun 11 '14 at 08:49
  • @Dennis_E On the .NET String type, `Equals` is the same as `==` except with the latter you don't have to do explicit null protection because you aren't actually invoking a method on the instance. [source](http://stackoverflow.com/a/5796219/486504) – user Jun 11 '14 at 08:52
  • 1
    @Maarten Well not exactly, it's in fact file.Name.Substring(0,4) shall equal to N1[j].Name. In my code it's like this. Here I just want to simplfy the code. – jcraffael Jun 11 '14 at 09:01
  • @jcraffael Will the if statement `if (file.Name== N1[j].Name)` then ever evaluate to `true` ? I think not. – Maarten Jun 11 '14 at 09:02

1 Answers1

1

The line

List<FileInfo>[] sublist = new List<FileInfo>[foldername.Length];

doesn't actually create an array with empty lists, it creates an array with NULL references. I think you should create an instance of an empty list, and store it in the array.

Maarten
  • 22,527
  • 3
  • 47
  • 68
  • Exactly! So my last sentence "sublist[j].Add(file)" is for populating them! – jcraffael Jun 11 '14 at 08:44
  • Small mistake on my part, you are creating an array with NULL references. I think you should create empty list instances, and set store them in the array. – Maarten Jun 11 '14 at 08:45
  • Maybe you are right. But that means I shall creat 7 times "Listsublist = new List()" and store them in an array like "List[]Sublist={sublist1, sublist2....sublist7}"? If so, that's really unconvenient especially when the length of array is huge – jcraffael Jun 11 '14 at 08:58
  • @jcraffael `for (int j = 0; j < foldername.Length; j++) { sublist[j] = new List(); ...` – Tzah Mama Jun 11 '14 at 08:58
  • Your code has two inputs: `list` and `Names`. `list` is a list/enumerable of `FileInfo`'s, and `Names` is a list of strings. Do they contain the same data? – Maarten Jun 11 '14 at 09:00
  • ok done! You are right about it. With Tzah's suggestion I made it. Thanks you very much – jcraffael Jun 11 '14 at 09:08