LINQ to Objects

LINQ to objects allows you to use LINQ queries on any collection that implements the IEnumerable interface.
This has a huge impact on developers who need to perform operations like search and sort on collections.

In order to demonstrate LINQ to Objects, we will write an application that allows the user to select a folder and an extension, and then list the files with the selected extension in the folder. Please note that providing a search pattern to Directory.GetFiles is much more efficient than the method I’ve used in this sample, but I’ve chosen to retrieve all the files first in order to filter the collection through LINQ.

I’ve chosen to create a .NET 3.5 WPF application in Visual Studio 2008.

When you’ve created the application, add a reference to System.Windows.Forms (this will allow you to use the FolderBrowserDialog class).

The XML for the window looks as follows:

 XAML

 

 

 

 

 

The following 3 functions allow the user to select the folder and populate the list of available file extensions:

private IEnumerable<FileInfo> getAllFiles(string sFolder)
        {
            //Get a list of files in the selected folder
            List<FileInfo> allFiles = new List<FileInfo>();
            string[] fileNames = Directory.GetFiles(sFolder);
            foreach (string name in fileNames)
            {
                allFiles.Add(new FileInfo(name));
            }
            return allFiles;
        }       
        private IEnumerable<string> getExtensions(IEnumerable<FileInfo> allfiles)
        {
            //Get a distinct list of file extensions
            IEnumerable<string> extensions =
                (from file in allfiles
                 orderby file.Extension
                 select file.Extension).Distinct();
            return extensions;
        }
        private void cmdSelectFolder_Click(object sender, RoutedEventArgs e)
        {
            //Select the folder to search and populate the combo box with a list of file extensions
            System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFolder.Text = folderBrowser.SelectedPath;
                cboExtensions.ItemsSource = getExtensions(getAllFiles(txtFolder.Text));
            }
        }

Now, when the user clicks the “Search” button, we will only list the files with the selected extension:

private void cmdSearch_Click(object sender, RoutedEventArgs e)
        {
            IEnumerable<FileInfo> allFiles;
            allFiles = getAllFiles(txtFolder.Text);
            IEnumerable<FileInfo> filteredFiles =
                (from file in allFiles
                 where file.Extension == (string)cboExtensions.SelectedItem
                 select file);
            lvwResults.ItemsSource = filteredFiles;
        }

0 Responses to “LINQ to Objects”



  1. Leave a Comment

Leave a Reply




Twitter Updates

del.icio.us