Linq 查询 - 多个 where,带有扩展方法
我的代码适用于 OR,正如我在下面列出的那样,但我想使用 AND 而不是 OR,但它失败了。我不太确定我做错了什么。基本上我有一个 Linq 查询,可以搜索 XML 文件中的多个字段。搜索字段可能并不全部都有信息。每个元素都运行扩展方法,并测试相等性。任何建议将不胜感激。
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
更新我弄清楚了到底是什么导致了这个问题。我仍然不知道如何解决它。基本上任何传递的空字符串都会导致操作失败。但是,如果所有(姓氏、名字等)都有标准,则它可以工作。有什么想法吗?
My code works for ORs as I've listed it below but I want to use AND instead of OR and it fails. I'm not quite sure what I'm doing wrong. Basically I have a Linq query that searches on multiple fields in an XML file. The search fields might not all have information. Each element runs the extension method, and tests the equality. Any advice would be appreciated.
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
Update I figured out what is exactly causing the problem. I still am not sure how to fix it. Basically any empty strings passed cause the operation to fail. However, if all (lastname, firstname, etc) have criteria it works. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其中一种测试方法似乎抛出异常。使用 OR 时,似乎不会调用失败的测试方法。
尝试找出哪一个中断,可能是因为要测试的值为 null 或属于非预期类型。
One of the test methods seems to throw an exception. When using OR the failing test method does not seemed to be called.
Try to find out which one breaks, maybe because the value to test is null or of a not expected type.