Posted By

ed_trench on 10/04/12


Tagged

Linq


Versions (?)

LINQ and Extension method (simple)


 / Published in: C#
 

simple LINQ and a Left extension method

  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  

You need to login to post a comment.