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

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to count file extensions and group it using LINQ. 
Test Data :
The files are : aaa.frx, bbb.TXT, xyz.dbf,abc.pdf
aaaa.PDF,xyz.frt, abc.xml, ccc.txt, zzz.txt
Expected Output :
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

الأجوبة

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();
        }
    }
	
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...