Processing Files Using Anonymous Types

Posted Wed, Aug 19 2009 14:06 by Deborah Kurata

Another use of anonymous types is for processing directories or files. For example, your application needs to collect a set of files and then process them based on a subset of the file properties.

[To begin with an overview of anonymous types, start here.]

Here is an example.

In C#:

// Query the set of files
var fileTemplateQuery = from FileInfo f in 
            new DirectoryInfo(@"C:\temp") 
            .GetFiles("*.*", SearchOption.AllDirectories) 
                 where f.Extension.ToLower() == ".xml" || 
                       f.Extension.ToLower() == ".txt" 
                 select new 
                 { 
                    DateLastModified = f.LastWriteTime, 
                    Extension = f.Extension, 
                    Size = f.Length, 
                    FileName = f.Name 
                 };

foreach (var f in fileTemplateQuery.OrderBy(file =>
                                    file.DateLastModified))
    // Do whatever you need to with the files
    Debug.WriteLine(f.FileName);

In VB:

Dim fileTemplateQuery = From f As FileInfo In _
            My.Computer.FileSystem.GetDirectoryInfo("C:\temp") _
            .GetFiles("*.*", SearchOption.AllDirectories) _
                 Where f.Extension.ToLower = ".xml" OrElse _
                       f.Extension.ToLower = ".txt" _
                 Select New With _
                 { _
                    .DateLastModified = f.LastWriteTime, _
                    .Extension = f.Extension, _
                    .Size = f.Length, _
                    .FileName = f.Name _
                  }

For Each f In _
   fileTemplateQuery.OrderBy(Function(file) file.DateLastModified)
    ' Do whatever you need to with the files
    Debug.WriteLine(f.FileName)
Next

This code uses Linq to find a specific set of files. In this case, it finds all of the files in C:\temp and its subdirectories where the extension is .xml or .txt. It then uses an anonymous type to retain the set of desired file properties.

The for/each statement loops through the set of anonymous types in order by date and performs whatever operation is required to process the files.

Enjoy!

Filed under: , , , ,

Comments

# re: Processing Files Using Anonymous Types

Wednesday, August 19, 2009 6:45 PM by CB

The "useful scope" of the anonymous type is pretty much what you can see on screen, right?  It seems that there is little you can do with the query result unless you know exactly what the

'select new {...}' statement looked like.

Or is that somehow discoverable?

These are very useful posts, thanks for putting them uip.

# Microsoft spotlight: MVP Deborah Kurata

Wednesday, August 19, 2009 6:55 PM by No1 Microsoft Fan

I’ve been keeping an eye on Deborah’s MSMVPS blog and am amazed. Deborah is posting some awesome code

# Interesting Finds: August 20, 2009

Thursday, August 20, 2009 6:41 AM by Jason Haley

Interesting Finds: August 20, 2009

# Processing Files Using Anonymous Types - Deborah Kurata

Thursday, August 20, 2009 9:48 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: Processing Files Using Anonymous Types

Thursday, August 20, 2009 11:49 AM by Bob Bingham

I don't get it.  Why strip the file objects down to just the four properties?  If you were going to pass the anonymous type out to other routines then I could see having security reasons to do something like that but anonymous types need to stay within the routine.  Wouldn't you rather just use the file types you already pulled with .GetFiles?

# re: Processing Files Using Anonymous Types

Thursday, August 20, 2009 11:54 AM by Deborah Kurata

Hi CB -

Yes, the useful scope of an anonymous type is primarily restricted to the routine in which it was defined.

If it had a more significant scope, a named type (basically a class) should be used instead.

Thanks for visiting my blog!

# re: Processing Files Using Anonymous Types

Thursday, August 20, 2009 12:05 PM by Deborah Kurata

Hi Bob -

Thanks for visiting my blog.

There are lots of properties on the file objects. If you had lots of code where I have "Do whatever ..." and that code only used the four properties, it might be helpful to only have the four properties showing up in Intellisense.

In *real* projects where I have used this, we have also defined properties that modified the built-in properties such as defining the .FileName property without an extension or removing the "." from the extension.

Hope this helps.

# Anonymous Types: An Introduction

Tuesday, October 20, 2009 11:56 PM by Deborah's Developer MindScape

The last several posts have provided examples of using anonymous types. This post backs up a little and

# re: Processing Files Using Anonymous Types

Wednesday, June 29, 2011 12:54 PM by Lurraine

Yup, that'll do it. You have my appreciatoin.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: