programming-examples/c-sharp/Linq/C# Program to Count File Extensions and Group it using LINQ.cs

32 lines
873 B
C#
Raw Normal View History

2019-11-15 12:59:38 +01:00
/*
* C# Program to Count File Extensions and Group it using LINQ
*/
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();
}
}
}
/*
4 File(s) with txt Extension
2 File(s) with pdf Extension
1 File(s) with xml Extension