如何讓LINQ到忽略是空的任何參數?所以姓,名,等等?如果我在所有參數正常工作數據...LINQ查詢忽略空參數
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
我會嘗試感謝你回答。 +1這樣一個很好的詳細的嘗試,我會報告,如果它的工作。再次感謝。 – 2010-04-20 20:07:22
好的,我在代碼中犯了一個錯誤。我真的想要「和」每個參數。當我在試圖讓它工作的時候,我從那裏得到了操作系統。所以最終我目前沒有得到任何結果。 (我更新了問題)我得到結果的唯一時間是如果所有參數都有。所以是的,它與任何參數中的空字符串有關。 – 2010-04-20 20:20:09
那麼,你想* TestPhoneElement *返回真或假在其中* value2compare *參數爲空字符串(或沒有)的情況下? – 2010-04-21 13:04:02