将数据表搜索转换为lambda表达式

时间:2014-05-12 18:02:14

标签: .net vb.net linq lambda ado.net

我需要帮助将这个for循环转换为VB.NET中的LINQ / Lambda表达式:

For Each dr As DataRow In dt.Rows
    Dim firstName As String = CStr(dr("FirstName")).ToLower()
    Dim lastName As String = CStr(dr("LastName")).ToLower()

    If Not String.IsNullOrEmpty(firstName) AndAlso Not String.IsNullOrEmpty(lastName) Then
        If firstName.ToLower = "john" AndAlso lastName.ToLower = "doe" Then
            Return "Found"
        End If
    End If
Next

Return "Not Found"

谢谢!

1 个答案:

答案 0 :(得分:3)

试试这个:

    Dim row = dt.AsEnumerable().Where(Function(f) f.Field(Of String)("FirstName").ToLower = "john" AndAlso f.Field(Of String)("LastName").ToLower = "doe").FirstOrDefault()

    Return If(row Is Nothing, "Not Found", "Found")