編輯:注意。我應該提到我對使用.Select,DataRowView,RowFind等不感興趣。感謝您提供這些建議,但代碼仍然存在,我希望能夠優化。自定義DataTable搜索
我寫了一個函數來搜索數據表中的數組中的術語。然後,我將數據表作爲數據集返回,以便它可以綁定到DataGridView。有沒有人有任何優化的想法?由於這是一個實時搜索(即時搜索輸入的每個術語),我希望儘可能快地完成該功能。速度實際上已經很不錯了,但是我想我應該問問你們,如果還有什麼我可以改變的話。
- VB.NET
- .NET 2.0
公共功能的搜索引擎(BYVAL條款()作爲字符串,爲ByRef ResidentTBL作爲數據表)作爲數據集 昏暗newdts作爲新的數據集( 「Users」) Dim newtable,temptable As New DataTable Dim residentPassCeiling,tempPassCeiling,termsceiling As Integer residentPassCeiling = ResidentTBL.R ows.Count - 1 termsceiling = Terms.GetUpperBound(0) newtable的= ResidentTBL.Clone
For j = 0 To termsceiling
If j < 1 Then
'######################## BEGIN First Pass ########################################################################
For i = 0 To residentPassCeiling
If ResidentTBL.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper = Terms(j).ToUpper Then
'we have an excact match on account skip further additions
newtable.Rows.Clear()
newtable.ImportRow(ResidentTBL.Rows(i))
newdts.Tables.Add(newtable)
Return newdts
Else
If _
ResidentTBL.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.displayName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.sn - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.givenName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.l - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.department - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.telephoneNumber - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
ResidentTBL.Rows(i).Item(TheColumns.building - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Then
'then add to the row
newtable.ImportRow(ResidentTBL.Rows(i))
End If
End If
Next i
'######################## END First Pass ########################################################################
Else
'######################## BEGIN Second.. Pass(es) ########################################################################
'Further searches we want to search the already filtered datatable from each term
temptable = newtable.Clone
temptable = newtable.Copy
newtable.Clear()
tempPassCeiling = temptable.Rows.Count - 1
For i = 0 To tempPassCeiling
If temptable.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper = Terms(j).ToUpper Then
'we have an excact match on account skip further additions
newtable.Rows.Clear()
newtable.ImportRow(temptable.Rows(i))
newdts.Tables.Add(newtable)
Return newdts
Else
If _
temptable.Rows(i).Item(TheColumns.AccountName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
temptable.Rows(i).Item(TheColumns.sn - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
temptable.Rows(i).Item(TheColumns.givenName - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
temptable.Rows(i).Item(TheColumns.l - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
temptable.Rows(i).Item(TheColumns.department - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
temptable.Rows(i).Item(TheColumns.telephoneNumber - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Or _
temptable.Rows(i).Item(TheColumns.building - 1).ToString.ToUpper Like Terms(j).ToUpper & "*" Then
'then add to the row
newtable.ImportRow(temptable.Rows(i))
End If
End If
Next i
temptable = Nothing
'######################## END Second.. Pass(es) ########################################################################
End If
Next j
newdts.Tables.Add(newtable)
ResidentTBL = Nothing
Return newdts
newdts = Nothing
End Function
文檔如果你不想考慮改變它,你可能已經在你的有關可用任何優化的答案。 – 2009-04-14 20:34:57