w3resource
C# Sharp Programming Exercises

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

C# Sharp LINQ : Exercise-15 with Solution

Write a program in C# Program 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 

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?



New Content: Composer: Dependency manager for PHP, R Programming