LINQ and Extension method (simple)


/ Published in: C#
Save to your folder(s)

simple LINQ and a Left extension method


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace CentreFolderDeduper
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. String[] centreFolders = Directory.GetDirectories(args[0]);
  14. List<String> centres = new List<String>();
  15.  
  16. foreach(var centreFolder in centreFolders)
  17. {
  18. if (centreFolder.IndexOf('-') != -1)
  19. {
  20. centres.Add(centreFolder.Left(centreFolder.IndexOf('-')));
  21. }
  22. }
  23.  
  24. var duplicateFolders = from f in centres
  25. group f by f.ToString() into g
  26. where g.Count() > 1
  27. select new { CentreFolderName = g.Key, CentreFolderNameCount = g.Count() };
  28.  
  29. Console.WriteLine(duplicateFolders.Count());
  30. ObjectDumper.Write(duplicateFolders);
  31. Console.ReadKey();
  32. }
  33. }
  34. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.