CompositeWPF / LINQPAD - Dissecting the GetModuleInfos(string path) method

LinkPad is a free utility that is available at http://www.LinqPad.net

I use the DirectlyLookupModuleEnumerator class for my SDMS application (blogged HERE and HERE) so when I stumbled across the following my first reaction was Foot in mouth followed by Yell --- the pit-bull side of me said "you have to understand what this is doing".

Enter stage left - LINQPAD!!!  I emulated the above LINQ Query, step-by-step, so that I could see what the query was doing.  You'll see I pretty much evolve the same lines (in below image) to see what is returned.  I was particularly interested in what line 185 was doing above.

For your convenience (COPY / PASTE)

/*000*/     string searchFolder = @"C:\Windows\System", fileFilter="*.DLL";
/*001*/   Directory.GetFiles(searchFolder, fileFilter).Dump("All files in folder");
/*002*/
/*003*/  string[] filter = {"avi","ole"}; // files we want to filter (include)

/*005*/  // Get all files in the debug folder
/*006*/  Directory.GetFiles(searchFolder, fileFilter)
/*007*/   // Each file in our filter will be processed against fileName
/*008*/   .Where(filterName => filter  // Note: Where clause expects bool result
/*009*/    .FirstOrDefault(fileName => filterName.ToLower().Contains(fileName))!=null).Dump("Files to be processed");

/*006a*/  Directory.GetFiles(searchFolder, fileFilter)
/*007a*/   // Each file in our filter will be processed against fileName
/*008a*/  .Where(filterName => filter  // Note: Where clause expects bool result
/*009a*/   .FirstOrDefault(fileName => filterName.ToLower().Contains(fileName))!=null)
/*010a*/         .SelectMany( file => file.Split('\\')
    ).Dump("SelectMany results without .Where() clause");

/*006b*/  Directory.GetFiles(searchFolder, fileFilter)
/*007b*/   // Each file in our filter will be processed against fileName
/*008b*/  .Where(filterName => filter  // Note: Where clause expects bool result
/*009b*/   .FirstOrDefault(fileName => filterName.ToLower().Contains(fileName))!=null)
/*010b*/         .SelectMany( file => file.Split('\\').Where(segment=> segment.Contains(".DLL"))
    ).Dump("SelectMany results with .Where() clause to only pull .DLL entries");

/*999*/
/*999*/  (filter.FirstOrDefault(listname => listname.Contains("ole"))      )
/*999*/   .Dump("Line 009 returns string if found without {!= null} which will crash .Where() clause");
/*999*/  (filter.FirstOrDefault(listname => listname.Contains("ole"))!=null)
/*999*/    .Dump("Line 009 returns bool with {!= null} which is what .Where() clause is expecting");
/*999*/

 


Tags: , , ,
Categories: CompositeWPF


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know