I’m using the following function call:
var filesfound = filterSplit.SelectMany(
filter => folder1.GetFiles(
filter,
SearchOption.AllDirectories
)
);
And I’d like to put a conditional statement in there that will change SearchOption.AllDirectories to SearchOption.TopDirectoryOnly based on a certain condition (A checkbox checked or not)
Is there a way to do this? (I can’t put the entire declaration into an if statement, even with an else option still declaring it, as it doesn’t allow me to, saying that filesfound does not exist…)
If you just want to use a conditional as an expression then use the ternary / conditional operator
Complete Sample
One other thing I noticed in your question is you tried to use a conditional statement block to wrap your expression. While this case can be solved without a statement there are occasions where using a statement is more natural. In those instances it’s often easier to move the statement into the lambda instead of surrounding the enclosing statement.