2010-04-19 86 views
0

我的代碼適用於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 

更新我想通了,究竟是什麼導致了問題。我仍然不確定如何解決它。基本上任何空字符串都會導致操作失敗。但是,如果所有(姓氏,名字等)都有它的標準,有任何想法嗎?

+1

而不是調用'ToLower',你應該通過'StringComparison.OrdinalIgnoreCase'。 – SLaks 2010-04-19 20:58:34

+0

謝謝。我不知道。 – 2010-04-20 14:54:12

回答

1

其中一種測試方法似乎會引發異常。當使用OR時,失敗的測試方法似乎沒有被調用。

嘗試找出哪個中斷,可能是因爲要測試的值爲null或不是預期類型。

+0

調試Linq似乎有點困難。我在查詢中放置了一個斷點,它只是一次運行整個事件。有更容易的方法嗎? – 2010-04-20 14:55:21