Linq query ignores empty parameters
How do I make Linq ignore any empty values? So Lastname, Firstname, etc.? If I have data on all parameters, it works fine ...
refinedresult = From x In theresult _
Where x.<thelastname>.Value.TestPhoneElement(LastName) And _
x.<thefirstname>.Value.TestPhoneElement(FirstName) And _
x.<id>.Value.TestPhoneElement(Id) And _
x.<number>.Value.TestPhoneElement(Telephone) And _
x.<location>.Value.TestPhoneElement(Location) And _
x.<building>.Value.TestPhoneElement(building) And _
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
a source to share
Just to make sure I understand what you want: you want the IEnumerable XElements x to return where at least one of the values of the child elements matched the corresponding string variable. Therefore, by ignoring, you are implying that your extension method will return false. So my deduction is that if it doesn't work fine, then an empty parameter returns true (wrongly) returned by TestPhoneElement, and hence you get false positives. Meaning, if the parameter is emptystring or nothing, it always returns true, and hence you get items in your result that you shouldn't be getting.
My thinking is this:
ret = parent.ToLower.StartsWith(value2compare.ToLower.Trim)
Can only return true.-
value2compare.ToLower.Trim()
will definitely cause the stated problem. -
String.IsNullOrEmpty(value2compare)
should return false.
I believe that the second parameter that you pass in TestPhoneElement
must actually be a string containing at least one space. Thus, it String.IsNullOrEmpty(value2compare)
returns false. Then the final line value2compare.ToLower.Trim
evaluates to an empty line because you trimmed it, and ret = parent.ToLower.StartsWith(value2compare.ToLower.Trim)
evaluates to true because each line starts with an empty line.
So trim2compare value on first entry, or changing 2nd conditional value to:
If String.IsNullOrEmpty(value2compare.trim()) Then
and you should be fine.
Edit:
solution based on the specified situation
You want the empty string to be passed to the extension method in order to get True, right? Also, I've updated the extension method to get slightly cleaner code. The key is that you want any empty line to pass in order to return True:
refinedresult = From x In theresult _
Where x.<thelastname>.MatchesOrIsBlank(LastName) And _
x.<thefirstname>.MatchesOrIsBlank(FirstName) And _
x.<id>.MatchesOrIsBlank(Id) And _
x.<number>.MatchesOrIsBlank(Telephone) And _
x.<location>.MatchesOrIsBlank(Location) And _
x.<building>.MatchesOrIsBlank(Building) And _
x.<department>.MatchesOrIsBlank(Department) _
Select x
and
Public Function TestPhoneElement(ByVal xE As XElement, ByVal value2compare As String) As Boolean
Return xE.Value.ToLower.StartsWith(value2compare.ToLower.Trim)
End Function
a source to share