VB.Net在数据表中搜索新的值(VB.Net search for new occurencies of value in datatable)

编程入门 行业动态 更新时间:2024-10-15 08:18:14
VB.Net在数据表中搜索新的值(VB.Net search for new occurencies of value in datatable)

我正在尝试在一个数据表中搜索字符串,因此如果出现新的命中,则会触发该操作。 怎么做?

我目前的代码:

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 If

the "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 bar

the 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 bar

Now 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 If

Found 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 .

更多推荐

本文发布于:2023-08-06 19:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表   Net   VB   datatable   occurencies

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!