Saturday, 2 December 2017

C# Program to Count File Extensions and Group it using LINQ


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication9
{
    class Program
    {
        public static void Main()
        {
            string[] arr = { "aaa.txt", "bbb.TXT", "xyz.abc.pdf", "aaaa.PDF", "abc.xml", "ccc.txt", "zzz.txt" };
            var egrp = arr.Select(file => Path.GetExtension(file).TrimStart('.').ToLower())
                     .GroupBy(x => x,(ext, extCnt) =>new
                                                     {
                                                        Extension = ext,
                                                        Count = extCnt.Count()
                                                      });

            foreach (var v in egrp)
                Console.WriteLine("{0} File(s) with {1} Extension ",v.Count, v.Extension);
            Console.ReadLine();
        }
    }
}


Output:

4 File(s) with txt Extension
2 File(s) with pdf Extension
1 File(s) with xml Extension


More C# Programs:




















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...