多个搜索条件(VB.NET)
所以我的问题是: 我有一个自定义类型{Id为Integer,Tag为String}的列表, 我想对它执行多条件搜索;如: SearchTags={Document,HelloWorld} 搜索结果将以以下格式放置在列表框ListBox1中: resultItem.id&&&resultItem.tags 我已经在论坛上尝试了所有我能找到的东西,但对我来说都不起作用——它适用于db或字符串数据类型 现在,我真的需要你的帮助。提前谢谢多个搜索条件(VB.NET),vb.net,search,Vb.net,Search,所以我的问题是: 我有一个自定义类型{Id为Integer,Tag为String}的列表, 我想对它执行多条件搜索;如: SearchTags={Document,HelloWorld} 搜索结果将以以下格式放置在列表框ListBox1中: resultItem.id&&&resultItem.tags 我已经在论坛上尝试了所有我能找到的东西,但对我来说都不起作用——它适用于db或字符串数据类型 现在,我真的需要你的帮助。提前谢谢 For Each MEntry As EntryType
For Each MEntry As EntryType In MainList
For Each Entry In MEntry.getTags
For Each item As String In Split(TextBox1.Text, " ")
If Entry.Contains(item) Then
If TestIfItemExistsInListBox2(item) = False Then
ListBox1.Items.Add(item & " - " & Entry.getId)
End If
End If
Next
Next
Next
自定义数组示例:
(24,{"snippet","vb"})
(32,{"console","cpp","helloworld"})
and so on...
我搜索了vb测试代码段:
snippet vb helloWorld - 2
snippet vb tcpchatEx - 16
cs something
test
因此,我将获取所有包含我的搜索短语的内容
我期望:
snippet vb tcp test
snippet vb dll test
snippet vb test metroui
所以,我想得到所有包含我所有搜索短语的内容
我的整个,代码类
Imports Newtonsoft.Json
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Dim MainList As New List(Of EntryType)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MainList.Clear()
Dim thr As New Threading.Thread(AddressOf thr1)
thr.SetApartmentState(Threading.ApartmentState.MTA)
thr.Start()
End Sub
Delegate Sub SetTextCallback([text] As String)
Private Sub SetTitle(ByVal [text] As String) ' source <> mine
If Me.TextBox1.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetTitle)
Me.Invoke(d, New Object() {[text]})
Else
Me.Text = [text]
End If
End Sub
Sub thr1()
Dim linez As Integer = 1
Dim linex As Integer = 1
For Each line As String In System.IO.File.ReadAllLines("index.db")
linez += 1
Next
For Each line As String In System.IO.File.ReadAllLines("index.db")
Try
Application.DoEvents()
Dim a As saLoginResponse = JsonConvert.DeserializeObject(Of saLoginResponse)(line) ' source <> mine
Application.DoEvents()
MainList.Add(New EntryType(a.id, Split(a.tags, " ")))
linex += 1
SetTitle("Search (loading, " & linex & " of " & linez & ")")
Catch ex As Exception
End Try
Next
SetTitle("Search")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim searchTags() As String = TextBox1.Text.Split(" ")
Dim query = MainList.Where(Function(et) et.Tags.Any(Function(tag) searchTags.Contains(tag))).ToList
For Each et In query
ListBox1.Items.Add(et.Id)
Next
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) ' test
MsgBox(Mid(ListBox1.SelectedItem.ToString, 1, 6)) ' test
End Sub 'test, removeonrelease
End Class
Public Class EntryType
Public Property Id As Integer
Public Property Tags() As String
Public Sub New(ByVal _id As Integer, ByVal _tags() As String)
Me.Id = Id
Me.Tags = Tags
End Sub
Public Function GetTags() As String
'to tell the Listbox what to display
Return Tags
End Function
Public Function GetId() As Integer
'to tell the Listbox what to display
Return Id
End Function
End Class
错误:
Debugger:Exception Intercepted: _Lambda$__1, Form2.vb line 44
An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occurred. "Unwind the call stack on unhandled exceptions" is selected in the debugger options.
Time: 13.11.2014 03:46:10
Thread:<No Name>[5856]
这里是一个Lambda查询。Where在谓词上进行筛选,因为标记是一个数组,所以可以使用Any函数基于另一个数组SearchTags执行搜索。您可以将每个类对象存储在列表框中,因为它存储了对象,您只需要告诉它下面要显示什么
Public Class EntryType
Public Property Id As Integer
Public Property Tags() As As String
Public Overrides Function ToString() As String
'to tell the Listbox what to display
Return String.Format("{0} - {1}", Me.Id, String.Join(Me.Tags, " "))
End Function
End Class
Dim searchTags = textbox1.Text.Split(" "c)
Dim query = mainlist.Where(Function(et) et.Tags.Any(Function(tag) searchTags.Contains(tag))).ToList
For Each et In query
Listbox1.Items.Add(et)
Next
你能给出一些db示例数据吗?它不是一个db,它是一个自定义类型的数组,id为integer,tag为StringGetID和GetTags函数是多余的,因为你可以直接调用属性-Fyit在最后一个注释中有几个问题,你能回答吗?我的应用程序在创建查询的行中崩溃了;值不能为空;见op;我需要获得与我的所有搜索查询匹配的所有结果,而不仅仅是其中的一个/+结果。我看到一个地方,您可以清除MainList-如果该查询为空,则该查询无法运行。我等待查询被填充。您是否调试并检查MainList变量,然后再运行它?
Public Class EntryType
Public Property Id As Integer
Public Property Tags() As As String
Public Overrides Function ToString() As String
'to tell the Listbox what to display
Return String.Format("{0} - {1}", Me.Id, String.Join(Me.Tags, " "))
End Function
End Class
Dim searchTags = textbox1.Text.Split(" "c)
Dim query = mainlist.Where(Function(et) et.Tags.Any(Function(tag) searchTags.Contains(tag))).ToList
For Each et In query
Listbox1.Items.Add(et)
Next