我的代碼適用於ORs,因爲我在下面列出它,但我想使用AND而不是OR,並且它失敗。我不太確定我做錯了什麼。基本上我有一個Linq查詢在XML文件中的多個字段上進行搜索。搜索字段可能並不都有信息。每個元素都運行擴展方法,並測試相等性。任何意見,將不勝感激。Linq查詢 - 多個在哪裏,使用擴展方法
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
更新我想通了,究竟是什麼導致了問題。我仍然不確定如何解決它。基本上任何空字符串都會導致操作失敗。但是,如果所有(姓氏,名字等)都有它的標準,有任何想法嗎?
而不是調用'ToLower',你應該通過'StringComparison.OrdinalIgnoreCase'。 – SLaks 2010-04-19 20:58:34
謝謝。我不知道。 – 2010-04-20 14:54:12