Search code examples
c#linqienumerabledirectoryinfo

Directory.GetDirectories with applied ExcusionsList with Linq


I have:

var folders = Directory.GetDirectories(path);

Decided to exclude some folders which they were listed in an array:

string[] ExcludingList;

And trying to achieve it via LINQ: So was trying something similar to the line below, but yet the format is wrong:

var folders = Directory.GetDirectories(path)
    .Where(d => (! ExcludingList.Contains(d.Name)));

I know it is still wrong, maybe an extra initialization, a type change, conversion, or changing a bit of format be needed, can you help me with it?


Solution

  • The code is good, but the issue is d represent the Name of sub directory, then you don't need to use d.Name, use just d, change little the code to :

    var folders = Directory.GetDirectories(path)
        .Where(d => !ExcludingList.Contains(d))
        .ToList();
    

    I hope you find this helpful.