【问题标题】:Convert Object Search Results into Function Class Values将对象搜索结果转换为函数类值
【发布时间】: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 语句。
  • 是的,这是复制和粘贴错误。

标签: vb.net linq datatable


【解决方案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();
}

【讨论】:

  • 你必须原谅任何 vb.net 语法问题,我已经有一段时间没有使用它了。
  • 好的,我现在就添加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 2018-02-06
  • 2019-01-24
相关资源
最近更新 更多