arraysvb.netlinqarraylist

LINQ Query to find items that contain a value in an array of values


I have a text property in a class that will contain multiple words along with other properties (index, linenumber, starttime, endtime). I have an arraylist (rawdata) of that class built.

I need to be able to create a list of words that that will be looked for independently in the text field for each member of the array.

The resultant members will then be displayed in a data grid view.

The text of the items in the array might look something like this. All text is already lower case, so I don't have to worry about case sensitivity. item 1 text "this has the word one" item 2 text "this has the word two and three" item 3 text "this has the word three" item 4 text "this has the word four" item 5 text "this has the word one and five"

This is what functions.

Private Sub filldgvData()
    Dim query = From item In rawdata
                Select item.index, item.LineNumber, item.StartTime, item.EndTime, item.Text
                Where Text.Contains("one")
                Order By LineNumber Ascending
    dgvdata.DataSource = query.ToList
    dgvdata.Columns(0).HeaderText = "Index"
    dgvdata.Columns(1).HeaderText = "Line"
    dgvdata.Columns(2).HeaderText = "Start Time"
    dgvdata.Columns(3).HeaderText = "End Time"
    dgvdata.Columns(4).HeaderText = "Text"

End Sub

This displays items 1 and 2 in the data grid view as expected.

However, the word I am looking for would actually be a list of words. I tried the below, searching for the words "one" "two" or "three". I don't have a definitive list of what the strSearch array would contain. It could be more or less than this list.

Private Sub filldgvData()
    Dim strSearch() As String = {"one", "two", "three"}
    Dim query = From item In rawdata
                Select item.index, item.LineNumber, item.StartTime, item.EndTime, item.Text
                Where Text.Contains(strSearch)
                Order By LineNumber Ascending
    dgvdata.DataSource = query.ToList
    dgvdata.Columns(0).HeaderText = "Index"
    dgvdata.Columns(1).HeaderText = "Line"
    dgvdata.Columns(2).HeaderText = "Start Time"
    dgvdata.Columns(3).HeaderText = "End Time"
    dgvSRT.Columns(4).HeaderText = "Text"

End Sub

This returns an error that says "Argument matching parameter 'value' cannot convert from 'String()' to 'String'." I understand what it is saying but can't wrap my head around how to fix this.

This should ideally return all items containing the words "one" or "two" or "three" (Items 1, 2, 3, and 5, but not 4). It should only return the item one time, so item 2 should not be listed twice even though it contains two of the words.


Solution

  • Instead of Where Text.Contains(strSearch), use Where strSearch.Any(Function(s) Text.Contains(s)). That basically says match where any of the items in strSearch will return True when passed to Text.Contains.

    That will find results that contain any of the search terms, so basically ORing them. If you want to AND them then use All instead of Any.