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

Add comment




biuquote
  • Comment
  • Preview
Loading



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