Linq query: { !<ObjectDisposedException> ... }

Hi need help having an error { ! … } , when using a variable in a Linq query for FileInfo(). sample code below:
System.IO.Directory.GetFiles(WorkFolderPath).Where(Function(s) New FileInfo(s).Name.ToUpper.Contains(varSamples) And New FileInfo(s).Name.ToUpper.Contains(varMsg))

but if I use the live strings like:
System.IO.Directory.GetFiles(WorkFolderPath).Where(Function(s) New FileInfo(s).Name.ToUpper.Contains(“MY SAMPLE EMAIL”) And New FileInfo(s).Name.ToUpper.Contains(“.MSG”))

there’s no Object Disposed error.

Thanks in advance for the help.

@jag.guiriba,

Try this approach.

Create a variable to store result of this System.IO.Directory.GetFiles(WorkFolderPath)
Once you have result in this variable like lstFiles then use LINQ to filter like

lstFiles.Where(Function(s) New FileInfo(s).Name.ToUpper.Contains(“MY SAMPLE EMAIL”) And New FileInfo(s).Name.ToUpper.Contains(“.MSG”))

Thanks,
Ashok :slight_smile:

Hi @jag.guiriba

Can you try the below syntax:

filteredFiles = System.IO.Directory.GetFiles(WorkFolderPath).Where(Function(s) New FileInfo(s).Name.ToUpper.Contains(varSamples.ToUpper()) AndAlso New FileInfo(s).Name.ToUpper.Contains(varMsg.ToUpper())).ToArray()

Make sure WorkFolderPath, varSamples, and varMsg are defined and assigned appropriately.

Regards

1 Like

Simplify the LINQ, add an conversion toarray or tolist at the end and Test IT within the immediate panel

filteredFiles = new DirectoryInfo(WorkFolderPath).GetFiles().Where(Function(f) f.Name.ToUpper.Contains(varSamples.ToUpper()) AndAlso f.Name.ToUpper.Contains(varMsg.ToUpper())).ToArray()

1 Like

it worked, thanks a lot

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.