我正在尝试在一个数据表中搜索字符串,因此如果出现新的命中,则会触发该操作。 怎么做?
我目前的代码:
If searchvalue <> "" Then foundRows = table.Select("Name LIKE '%" & searchvalue & "%'") If foundRows.Length = 0 Then 'none found Else For Each r In foundRows notif("Found "&r.itemarray(0) & " in " & r.itemarray(1)) Next End If End If每次调用sub时,每次命中都会调用“notif”函数; 但我希望每次独特的打击都会被召唤一次。 怎么做?
使用大小写:比如说,当表格如下时,第一次调用sub:
something foo smthelse bar搜索字符串是“some”,Notif称之为“some foo”。 下次调用sub时表就是这样的:
something foo something else smthelse bar现在Notif只应该被称为“别的东西”
I'm trying to search one datatable for a string, so if new hits are appeared the action is triggered. How to do that?
My current code:
If searchvalue <> "" Then foundRows = table.Select("Name LIKE '%" & searchvalue & "%'") If foundRows.Length = 0 Then 'none found Else For Each r In foundRows notif("Found "&r.itemarray(0) & " in " & r.itemarray(1)) Next End If End Ifthe "notif" function is called for every hit each time the sub is called; but i want it to be called once for every unique hit. How to do it?
Using case: say, first time sub is called when table is like that:
something foo smthelse barthe search string is "some", the Notif called once for "something foo". Next time the sub is called table is like that:
something foo something else smthelse barNow Notif should be called only for "something else"
最满意答案
我建议使用Linq-To-DataSet ,这样可以使代码更具可读性和可维护性,并且还具有一些很好的功能,如分组:
If searchvalue.Length <> 0 Then Dim foundNamegroups = From row In table Let Name = row.Field(Of String)("Name") Where Name.Contains(searchvalue) Group row By Name Into NameGroup = Group If foundNamegroups.Any() Then For Each grp In foundNamegroups ' note that the DataRows are in grp.NameGroup Dim msg = String.Format("Found {0} rows for name {1}", grp.NameGroup.Count(), grp.Name) notif(msg) Next End If End IfFound the solution - i used the row.RowState property.
Each time the row is changed or added, its row.RowState property equals either DataRowState.Added or DataRowState.Modified, and when the row.AcceptChanges() called it becomes DataRowState.Unchanged .
更多推荐
发布评论