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
How do I get Linq to ignore any parameters that are empty? So Lastname, Firstname, etc? If I have data in 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了确保我理解您想要什么:您想要返回 XElements x 的 IEnumerable,其中至少有一个子元素的值与相应的字符串变量匹配。因此,忽略意味着您的扩展方法将返回false。所以我推断,如果它不能正常工作,那么空参数会导致 TestPhoneElement (错误地)返回 true,因此您会得到误报。这意味着,如果参数是空字符串或什么都没有,它总是返回 true,因此您会在结果中获得不应该获得的项目。
我的想法是这样的:
value2compare.ToLower.Trim()
肯定会导致您指出的问题。String.IsNullOrEmpty(value2compare)
必须返回 false。我相信您传递给
TestPhoneElement
的第二个参数实际上必须是一个至少包含一个空格的字符串。这样,< code>String.IsNullOrEmpty(value2compare) 返回 false。然后,在最后一行,value2compare.ToLower.Trim
计算结果为空字符串,因为您修剪了它,并且ret =parent.ToLower.StartsWith(value2compare.ToLower.Trim)
计算结果为 true,因为每个字符串都以空字符串开头。因此,在 value2compare 第一次出现时修剪它,或者将第二个条件更改为:
并且你应该很好。
编辑:
基于澄清情况的解决方案
您希望将空字符串传递到扩展方法中以产生 True,对吗?另外,我更新了扩展方法,以允许更简洁的代码。但关键是您希望传入的任何空白字符串都会导致返回 True:
并且:
Just to make sure I understand what you want: You want an IEnumerable of XElements x returned where at least one of the child elements' values matching the corresponding string variable. So by ignore you mean that your extension method would return false. So I deduce that if it's not working fine, then an empty parameter causes true to (mistakenly) be returned by TestPhoneElement, and hence you get false positives. Meaning, if a parameter is an emptystring or nothing, it always returns true, and hence you are getting items in your result that you shouldn't be getting.
My thinking is this:
ret = parent.ToLower.StartsWith(value2compare.ToLower.Trim)
could possibly return true.value2compare.ToLower.Trim()
would certainly cause the problem you indicate.String.IsNullOrEmpty(value2compare)
has to be returning false.I believe that the second parameter that you're passing into
TestPhoneElement
must in fact be a string containing at least one space. This way,String.IsNullOrEmpty(value2compare)
returns false. Then in the final linevalue2compare.ToLower.Trim
evaluates to an empty string because you trimmed it, andret = parent.ToLower.StartsWith(value2compare.ToLower.Trim)
evaluates to true because every string begins with an empty string.So, trim value2compare when it first comes in, or change you 2nd conditional to:
and you should be good.
Edit:
solution based on clarified situation
You want an empty string passed into the extension method to result in True then, right? Also, I updated the extension method to allow for slightly cleaner code. The key though is that you want any blank string passed in to result in True being returned:
And: