Linq query - multiple where, with extension method
- by Cj Anderson
My code works for ORs as I've listed it below but I want to use AND instead of OR and it fails. I'm not quite sure what I'm doing wrong. Basically I have a Linq query that searches on multiple fields in an XML file. The search fields might not all have information. Each element runs the extension method, and tests the equality. Any advice would be appreciated.
refinedresult = From x In theresult _
Where x.<thelastname>.Value.TestPhoneElement(LastName) Or _
x.<thefirstname>.Value.TestPhoneElement(FirstName) Or _
x.<id>.Value.TestPhoneElement(Id) Or _
x.<number>.Value.TestPhoneElement(Telephone) Or _
x.<location>.Value.TestPhoneElement(Location) Or _
x.<building>.Value.TestPhoneElement(building) Or _
x.<department>.Value.TestPhoneElement(Department) _
Select x
Public Function TestPhoneElement(ByVal parent As String, ByVal value2compare As String) As Boolean
'find out if a value is null, if not then compare the passed value to see if it starts with
Dim ret As Boolean = False
If String.IsNullOrEmpty(parent) Then
Return False
End If
If String.IsNullOrEmpty(value2compare) Then
Return ret
Else
ret = parent.ToLower.StartsWith(value2compare.ToLower.Trim)
End If
Return ret
End Function