In this article, we will write a C# program to count file extensions and Group it using LINQ
Output:
4 file(s) with txt Extension
2 file(s) with pdf Extension
1 File(s) with xml Extension
2 file(s) with pdf Extension
1 File(s) with xml Extension
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace LinqSamples
{
public class Program
{
public static void Main()
{
string[] arr = { "sample.txt", "sample1.TXT", "sample.test.pdf", "sample1.PDF", "sample.xml", "sample2.txt", "sample3.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();
}
}
}
|
Innovating the future with AI, Web Development, Data Science and Technology Solutions.
Worldwide Clients
Active Clients
Projects Completed
Cups of Coffee
Lorem Ipsum has been the industry's standard dummy text.

Top Blog for all kinds of information
0 Comments
If you have any doubts,please let me know