I have a form with a CheckedListBox where the user can select multiple options (it’s filtering down a directory filelist – the filtered files appear in a second listbox).
If I had a set number of options I could use “Or” in the query like this:
Dim query = From f As System.IO.FileInfo In StartList
Where f.Name.Contains("Romania")
Where f.Channel = "Channel1" Or f.Channel = "Channel2"
But as the person can click any number of options (or none) I can’t do this.
I’ve tried a couple of things including trying to create multiple sub-queries and then performing a Union on them but I can’t get it to work.
This is my code so far:
Dim Channels = ChannelListBox.CheckedItems
Dim query = From f As System.IO.FileInfo In StartList
Where f.Name.Contains("Romania")
If Channels.Count > 0 Then
Dim CurrentName As String = Channels(0).ToString
Dim Subquery = From g As System.IO.FileInfo In query
Where (g.Name.Contains(CurrentName))
For i = 1 To Channels.Count - 1
CurrentName = Channels(i).ToString
Dim Subquery2 = From h As System.IO.FileInfo In query
Where (h.Name.Contains(CurrentName))
Subquery = Subquery.Union(Subquery2)
Next i
FileListBox.DataSource = Subquery.ToList
Else
FileListBox.DataSource = query.ToList
End If
Thanks for your help,
Andrew
I don’t have VB.NET experience, let alone with LINQ, so I hope my syntax is correct. Here’s what I would write in C#: