【发布时间】:2015-07-15 17:39:39
【问题描述】:
我为我的客户开设了一堂课
Public Class CustomerClass
Public Email As String
Public MobilePhone As String
End Class
我将所有客户加载到内存中的数据表中
Public Shared Function LoadCustomers(Commodity As String) As DataTable
LoadCustomers = New DataTable
With LoadCustomers
.Columns.Add("Email", Type.GetType("System.String"))
.Columns.Add("MobilePhone", Type.GetType("System.String"))
End With
Dim MyRow As DataRow
Using connection As New SqlConnection(My.Settings.db)
Using command As New SqlCommand("GetCustomers", connection)
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = My.Settings.Command_Timeout
connection.Open()
Using reader As SqlDataReader = command.ExecuteReader()
While (reader.Read())
MyRow = LoadCustomers.NewRow()
With MyRow
.Item("Email") = If(IsDBNull("Email"), Nothing, reader("Email"))
.Item("MobilePhone") = If(IsDBNull("MobilePhone"), Nothing, reader("MobilePhone"))
End With
LoadCustomers.Rows.Add(MyRow)
End While
End Using
End Using
End Using
End Function
然后,我在数据表中搜索特定客户,但我不知道如何将搜索结果分配回函数类
问题:如何将搜索结果分配回函数类?
Public Shared Function MatchLoadedCustomers(CustomerDataTable As DataTable, CustomerToSearch As CustomerClass) As CustomerClass
MatchLoadedCustomers = New CustomerClass
Dim matches = From row In AuditDataTable
Let SortCode = row.Field(Of String)("Email")
Where SortCode = CustomerToSearch.Email.FirstOrDefault
If matches.Count > 0 Then
With MatchLoadedCustomers
'HERE IS THE PROBLEM
.Email = matches.fieldname("Email") 'does not work
.MobilePhone = matches.fieldname("MobilePhone") 'does not work
End With
End If
End Function
【问题讨论】:
-
可能像
matches(0).Field(Of String)("Email")这样的东西更接近你想要的。但是,看起来MatchLoadedCustomers从未初始化(而是MatchPortalAccountToNRA?)而且我在任何地方都看不到AuditDataTable,所以我怀疑也存在一些复制/粘贴问题。我还建议使用明确的Return语句。 -
是的,这是复制和粘贴错误。