Question

I need help converting this for loop to a LINQ/Lambda expression in VB.NET:

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"

Thanks!

Was it helpful?

Solution

Try this:

    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")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top