Home

Search in Files -

I have this snippet that lets me search in files.
var DirInfo = new DirectoryInfo(@"c:\Reports\PdfOutputs");

var files = from file in DirInfo.EnumerateFiles()
			where file.Name.Contains("128622") || file.Name.Contains("128241")
			|| file.Name.Contains("127361") || file.Name.Contains("127078")
			|| file.Name.Contains("126720") || file.Name.Contains("126590")
			|| file.Name.Contains("124854") || file.Name.Contains("129957")
            select new { file.Name, file.CreationTimeUtc };

files.Dump();
Instead of using file.Name.Contains multiple times, is there a way to do it by having all the names in a list?

Comments

  • You're not searching in files but rather for files (in a directory) :)
    var DirInfo = new DirectoryInfo(@"c:\Reports\PdfOutputs");
    
    var files = from file in DirInfo.EnumerateFiles()
                where new[] { "128622", "128241", "127361", "127078", "126720", "126590", "124854", "129957"}.Any(x => file.Name.Contains(x))
                select new { file.Name, file.CreationTimeUtc };
    
    files.Dump();
    I use this pattern frequently in LINQ to SQL to mimic WHERE ... IN (..., ...) syntax:
    where new[] { "a", "b" }.Contains(x.Field)
Sign In or Register to comment.