Sorting Files Using LINQ & Lambda Expressions

by Michael 30. December 2008 13:41


using System.Collections.Generic;
using System.IO;
using System.Linq;

class SortFiles
{
    public IEnumerable<FileInfo> Sort()
    {
        // Set the directory you want to search
        DirectoryInfo di = new DirectoryInfo(@"C:\"); 

        // Get all of the .jpg files from the directory and order
        // them by file name in descending order
        IEnumerable<FileInfo> files =
            di.GetFiles("*.jpg").OrderByDescending(e => e.Name); 

        return files;
    }
}

You can sort it by name, creation time, length, or any other FileInfo property.  You can find many other LINQ examples here:  http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

Tags: , ,

.NET | C# | LINQ

Comments

8/17/2010 3:23:20 AM #

I am amazed at how great the stuff is on this web page. I have written down this website and I really  plan on coming back to the site in the next few days. Great job keep up the excellent work!

paid for click

8/31/2010 6:30:16 PM #

Intimately, the post is actually the freshest on this valuable topic. I concur with your conclusions and will eagerly look forward to your coming updates. Saying thanks will not just be sufficient, for the phenomenal lucidity in your writing. I will at once grab your rss feed to stay privy of any updates. Solid work and much success in your business efforts!

Jerrie

9/4/2010 3:14:20 AM #

Looking to enjoy The Office totally free online? The tv series has won several academy awards! Here is a great website I found which lets you watch all the episodes from The Office free of charge. Its simple and easy to use and really quick.

Grant Freight

Add comment




biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Original Theme by Mads Kristensen | Modified by Crafty Coders