Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

August 19, 2009

Processing Files Using Anonymous Types

Filed under: C#,LINQ,VB.NET @ 4:06 pm

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 =>
    // Do whatever you need to with the files

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

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.



  1.   CB — August 19, 2009 @ 6:45 pm    Reply

    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.

  2.   Bob Bingham — August 20, 2009 @ 11:49 am    Reply

    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?

  3.   DeborahK — August 20, 2009 @ 11:54 am    Reply

    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!

  4.   DeborahK — August 20, 2009 @ 12:05 pm    Reply

    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.

  5.   Lurraine — June 29, 2011 @ 12:54 pm    Reply

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

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2022 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs