admin管理员组文章数量:1401620
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.
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.
Share Improve this question asked Mar 24 at 5:09 James ParksJames Parks 132 bronze badges1 Answer
Reset to default 2Instead 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
.
本文标签: vbnetLINQ Query to find items that contain a value in an array of valuesStack Overflow
版权声明:本文标题:vb.net - LINQ Query to find items that contain a value in an array of values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744260386a2597685.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论