Cara mengelompokkan file berdasarkan ekstensi (LINQ) (C#)

Contoh ini menunjukkan cara LINQ digunakan untuk melakukan operasi pengelompokan dan pengurutan lanjutan pada daftar file atau folder. Contoh ini juga menunjukkan cara membagi keluaran ke dalam jendela konsol dengan menggunakan metode Skip dan Take.

Contoh

Kueri berikut ini menunjukkan cara mengelompokkan konten pohon direktori tertentu berdasarkan ekstensi nama file.

class GroupByExtension  
{  
    // This query will sort all the files under the specified folder  
    //  and subfolder into groups keyed by the file extension.  
    private static void Main()  
    {  
        // Take a snapshot of the file system.  
        string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\Common7";  
  
        // Used in WriteLine to trim output lines.  
        int trimLength = startFolder.Length;  
  
        // Take a snapshot of the file system.  
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);  
  
        // This method assumes that the application has discovery permissions  
        // for all folders under the specified path.  
        IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);  
  
        // Create the query.  
        var queryGroupByExt =  
            from file in fileList  
            group file by file.Extension.ToLower() into fileGroup  
            orderby fileGroup.Key  
            select fileGroup;  
  
        // Display one group at a time. If the number of
        // entries is greater than the number of lines  
        // in the console window, then page the output.  
        PageOutput(trimLength, queryGroupByExt);  
    }  
  
    // This method specifically handles group queries of FileInfo objects with string keys.  
    // It can be modified to work for any long listings of data. Note that explicit typing  
    // must be used in method signatures. The groupbyExtList parameter is a query that produces  
    // groups of FileInfo objects with string keys.  
    private static void PageOutput(int rootLength,  
                                    IEnumerable<System.Linq.IGrouping<string, System.IO.FileInfo>> groupByExtList)  
    {  
        // Flag to break out of paging loop.  
        bool goAgain = true;  
  
        // "3" = 1 line for extension + 1 for "Press any key" + 1 for input cursor.  
        int numLines = Console.WindowHeight - 3;  
  
        // Iterate through the outer collection of groups.  
        foreach (var filegroup in groupByExtList)  
        {  
            // Start a new extension at the top of a page.  
            int currentLine = 0;  
  
            // Output only as many lines of the current group as will fit in the window.  
            do  
            {  
                Console.Clear();  
                Console.WriteLine(filegroup.Key == String.Empty ? "[none]" : filegroup.Key);  
  
                // Get 'numLines' number of items starting at number 'currentLine'.  
                var resultPage = filegroup.Skip(currentLine).Take(numLines);  
  
                //Execute the resultPage query  
                foreach (var f in resultPage)  
                {  
                    Console.WriteLine("\t{0}", f.FullName.Substring(rootLength));  
                }  
  
                // Increment the line counter.  
                currentLine += numLines;  
  
                // Give the user a chance to escape.  
                Console.WriteLine("Press any key to continue or the 'End' key to break...");  
                ConsoleKey key = Console.ReadKey().Key;  
                if (key == ConsoleKey.End)  
                {  
                    goAgain = false;  
                    break;  
                }  
            } while (currentLine < filegroup.Count());  
  
            if (goAgain == false)  
                break;  
        }  
    }  
}  

Output dari program ini dapat berukuran panjang, tergantung pada detail sistem file lokal dan pengaturan startFolder. Untuk mengaktifkan tampilan semua hasil, contoh ini menunjukkan cara membagi melalui hasil. Teknik yang sama dapat diterapkan ke aplikasi Windows dan Web. Perhatikan bahwa karena kode membagi item ke dalam grup, perulangan foreach bersarang diperlukan. Ada juga beberapa logika tambahan untuk mengkomputasi posisi saat ini dalam daftar, dan untuk memungkinkan pengguna agar dapat menghentikan pengelompokan dan keluar dari program. Dalam kasus khusus ini, kueri pengelompokan akan dijalankan terhadap cache hasil dari kueri asli. Dalam konteks lain, seperti LINQ untuk SQL, penembolokan tersebut tidak diperlukan.

Mengompilasi Kode

Buat proyek aplikasi konsol C#, dengan arahan using untuk namespace layanan System.Linq dan System.IO.