Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Count file extensions and group it

C# Sharp LINQ : Exercise-15 with Solution

Write a program in C# Sharp to count file extensions and group it using LINQ.

Sample Solution:-

C# Sharp Code:

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

class LinqExercise15
    {
        public static void Main()
        {
            string[] arr1 = { "aaa.frx", "bbb.TXT", "xyz.dbf","abc.pdf", "aaaa.PDF","xyz.frt", "abc.xml", "ccc.txt", "zzz.txt" };

            Console.Write("\nLINQ : Count file extensions and group it : "); 
            Console.Write("\n------------------------------------------\n");
            
            Console.Write("\nThe files are : aaa.frx, bbb.TXT, xyz.dbf,abc.pdf");
            Console.Write("\n                aaaa.PDF,xyz.frt, abc.xml, ccc.txt, zzz.txt\n");
            
            Console.Write("\nHere is the group of extension of the files : \n\n");
			
            var fGrp = arr1.Select(file => Path.GetExtension(file).TrimStart('.').ToLower())
                     .GroupBy(z => z,(fExt, extCtr) =>new
                                                     {
                                                        Extension = fExt,
                                                        Count = extCtr.Count()
                                                      });
 
            foreach (var m in fGrp)
                Console.WriteLine("{0} File(s) with {1} Extension ",m.Count, m.Extension);
            Console.ReadLine();
        }
    }
	

Sample Output:

LINQ : Count File Extensions and Group it :                                                                   
------------------------------------------                                                                        
The files are : aaa.frx, bbb.TXT, xyz.dbf,abc.pdf                                                             
                aaaa.PDF,xyz.frt, abc.xml, ccc.txt, zzz.txt                                                   
      
Here is the group of extension of the files :                                                                 
      
1 File(s) with .frx Extension                                                                                 
3 File(s) with .txt Extension                                                                                 
1 File(s) with .dbf Extension                                                                                 
2 File(s) with .pdf Extension                                                                                 
1 File(s) with .frt Extension                                                                                 
1 File(s) with .xml Extension 

Pictorial Presentation:

C# Sharp LINQ: Count File Extensions and Group it.

Flowchart:

Flowchart: LINQ : Count File Extensions and Group it

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find the n-th Maximum grade point achieved by the students from the list of students.
Next: Write a program in C# Sharp to Calculate Size of File using LINQ.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.