Using c# .NET 4.6.1 I have a group of strings I want to use to select "valid" files from a given directory. Here's what I mean:
Here is an example of the strings I want to use to get expected filenames from a directory that are stored in an array. I don't care if the values are stored in an array or a list or some other kind of collection. I can put them in whatever kind of collection that would work best for searching for the file names I want.
//"ValidValues" doesn't have to be an array,
//it can be a list/array/datatable or some other kind of collection
string[] ValidValues = new string[2] {"ab","cd"};
Files in the directory:
file_1_.txt
file_ab_.blah
file_2_.something
file_cd_.blo
etc.
I want to search for the filenames in the directory using the ValidValues
collection that contains the strings I want to use as my search criteria. I've gotten filenames out of a directory in the past using this:
string[] ValidFiles = Directory.GetFiles(@"C:\Drop");
But I don't know how to specify a filter that uses values contained in a collection to be searched for in the filenames in a directory. I know that there are lambda expressions that might be able to do this but I'm not quite sure how to use them. After the code I'm looking for is run I would expect to have
file_ab_.blah
and
file_cd_.blo
in ValidFiles
.
I've seen some posts where the search criteria is hard coded and separated by "||" or others where the collection of search strings is looped through but I want to make this dynamic by using ValidValues
to find filenames in my directory that contain any of the values in ValidValues
. I'm getting the strings contained in ValidValues
from a database.
Hopefully this makes sense, I have a strong feeling that it can be done I just don't know the exact syntax to accomplish this. I don't care how it's done, using LINQ or whatever else, I just want the most elegant and efficient method possible. If possible I'd like to avoid looping through the collection of strings I get out of the database and instead use the collection of strings I get out of the database as search criteria in a single statement, possibly using something like a lambda expression or something of the like. Thanks in advance.