我为我的客户上课
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
答案 0 :(得分:1)
虽然我总是强烈建议永远不要使用DataTables,因为你已经走了这条道路:
Public Shared Function MatchLoadedCustomers(CustomerDataTable As DataTable, CustomerToSearch As CustomerClass) As CustomerClass
MatchPortalAccountToNRA = New CustomerClass
Dim match = (From row In AuditDataTable
Let SortCode = row.Field(Of String)("Email")
Where SortCode = CustomerToSearch.Email).FirstOrDefault
If matches!=null Then
With MatchPortalAccountToNRA
.Email = match.Field(Of String)("Email")
.MobilePhone = match.Field(Of String)("MobilePhone")
End With
End If
Return MatchPortalAccountToNRA
End Function
或
Public Shared Function MatchLoadedCustomers(CustomerDataTable As DataTable, CustomerToSearch As CustomerClass) As CustomerClass
' Remove DataTable
Dim Customers = CustomerDataTable.Select(Function(x) New CustomerClass With {
.Email=x.Field(Of String)("Email"),
.MobilePhone=x.Field(Of String)("MobilePhone")})
' Now to real work
Return Customers.Where(Function(x) x.Email=CustomerToSearch.Email).FirstOrDefault
End Function
在C#中:
public static CustomerClass MatchLoadedCustomers(DataTable CustomerDataTable,
CustomerClass customerToSearch)
{
// Remove DataTable
var customers = CustomerDataTable
.Select(x=>new CustomerClass {
Email=x.Field<string>("Email"),
MobilePhone=x.Field<string>("MobilePhone")
});
// Now to real work
return customers
.Where(x=>x.Email==customerToSearch.Email)
.FirstOrDefault();
}
在C#中重构:
public static CustomerClass MatchLoadedCustomers(DataTable CustomerDataTable,
CustomerClass customerToSearch)
{
// Remove DataTable
var customers = CustomerDataTable
.Select(x=>new CustomerClass {
Email=x.Field<string>("Email"),
MobilePhone=x.Field<string>("MobilePhone")
});
// Now to real work
return MatchLoadedCustomers(customers,customerToSearch);
}
public static CustomerClass MatchLoadedCustomers(IEnumerable<CustomerClass> customers,
CustomerClass customerToSearch)
{
return customers
.Where(x=>x.Email==customerToSearch.Email)
.FirstOrDefault();
}