Suppose I have a list of strings. These strings will be a part of the directory name that I want to open.
var listOfStrings = new List<string>(){"Foo", "Bar", "LocalHost", "SomeIPAddress"};
If this is my list, my directories might look like this:
- Foo_TodaysDate_ThisFileNameIsMostlyLongAndUnhelpful
- Bar_TodaysDate_ThisFileNameIsMostlyLongAndUnhelpful
- LocalHost_TodaysDate_ThisFileNameIsMostlyLongAndUnhelpful
- SomeIPAddress_TodaysDate_ThisFileNameIsMostlyLongAndUnhelpful
So I have the code here to load the directory info into a list:
m_jsonDirectories = new DirectoryInfo(@"C:\ProgramData\SCLDDB\ReportLogs\")
.GetDirectories()
.OrderByDescending(p_f => p_f.LastWriteTime)
.ToList();
Right now, I can load all the directories in the master directory into my variable, but I want to add something like:
.Where(x => x.Name.Contains(/*A string found in my List above*/)
Edit: in the above statement, the parameter x is of type DirectoryInfo. So x.Name should return the Name of the Directory.
I don't know how to search
List.Any(s => string.Contains(s))
when I don't have a string variable already set. And ideally I'd just want to search each element of my list for a match without individually setting some temporary string variable.
.Where(x => listOfStrings.Any(s => x.Name.Contains(s)));
s
is of typestring
.var listOfStrings = new List<string>(){"Foo", "Bar", "LocalHost", "SomeIPAddress"};
like you've shown in your question.m_jsonDirectories.Where(x => listOfStrings.Any(s => x.Name.Contains(s)))