LINQ example: listing directories

LINQ is a valuable feature for database and XML programming, but not only. Basically, you can use LINQ with everything that returns an IEnumerable. Here is an example, when I want to list the directories of a parent directory, in alfabetical order, and grouped by the file attributes (first all that have only the flag Directory set, then those that have both ReadOnly and Directory, etc.).

First, let’s try how you would put this clasically in C# 2.0. Basically you need these:

  • obtain a list of directory names
  • create a sorted dictionary with the key representing the file attributes, and the value a list of directories
  • iterate over the list of names and create a DirectoryName object
  • add the object to the list corresponding to the key represented by the directory attributes
  • iterate over the dictionary and print its content

The code I wrote for that looks like this:

With LINQ, things get much simpler, as you no longer need to create a dictionary. You can apply SQL-like queries on objects. What you have to do is:

  • obtain a list of directory names
  • create a query to select the directories and group them according to their attributes
  • iterate over the result and print the its content

This is how it looks like:

Let’s take the querry line by line and see what it does:

  • from dir in from e in entries select new DirectoryInfo(e)creates a DirectoryInfo for each element of entries and uses the resulted array as source
  • orderby dir.Nameorders the DirectoryInfo array by the name of the directories
  • group dir by dir.Attributes into dirGroupsgroups the directories by their attributes into dirGroups
  • orderby dirGroups.Keyorders the group by the key
  • select dirGroupsselects the groups

And that’s all. You don’t have to struggle with creating dictionaries, maintaining the correct sorting and so on. You can do all of that with a simple query.

The following listing shows a second query that displays the directories ascending by their creation time, and for each directory the name, creation time and attributes are listed. The code also exemplifies the use of anonymous types, and the inference of the type from the assigned value, concepts new in C# 3.0:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.