C# Program to Count File Extensions and Group it using LINQ (JNNC Technologies)
In this article, we will write a C# program to count file extensions and Group it using LINQ
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();
}
}
}
|
Output:
4 file(s) with txt Extension
2 file(s) with pdf Extension
1 File(s) with xml Extension
';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
0 Comments
If you have any doubts,please let me know