Linq Query ignore empty parameters
Posted
by Cj Anderson
on Stack Overflow
See other posts from Stack Overflow
or by Cj Anderson
Published on 2010-04-20T19:04:25Z
Indexed on
2010/04/20
20:03 UTC
Read the original article
Hit count: 235
linq-to-xml
|vb.net
How do I get Linq to ignore any parameters that are empty? So Lastname, Firstname, etc? If I have data in all parameters it works fine.
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
© Stack Overflow or respective owner