Linq 查询忽略空参数

发布于 2024-08-29 23:17:04 字数 1142 浏览 3 评论 0原文

如何让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-09-05 23:17:04

只是为了确保我理解您想要什么:您想要返回 XElements x 的 IEnumerable,其中至少有一个子元素的值与相应的字符串变量匹配。因此,忽略意味着您的扩展方法将返回false。所以我推断,如果它不能正常工作,那么空参数会导致 TestPhoneElement (错误地)返回 true,因此您会得到误报。这意味着,如果参数是空字符串或什么都没有,它总是返回 true,因此您会在结果中获得不应该获得的项目。

我的想法是这样的:

  1. 只有 ret =parent.ToLower.StartsWith(value2compare.ToLower.Trim) 可能返回 true。
  2. value2compare.ToLower.Trim() 肯定会导致您指出的问题。
  3. String.IsNullOrEmpty(value2compare) 必须返回 false。

我相信您传递给TestPhoneElement的第二个参数实际上必须是一个至少包含一个空格的字符串。这样,< code>String.IsNullOrEmpty(value2compare) 返回 false。然后,在最后一行, value2compare.ToLower.Trim 计算结果为空字符串,因为您修剪了它,并且 ret =parent.ToLower.StartsWith(value2compare.ToLower.Trim) 计算结果为 true,因为每个字符串都以空字符串开头

因此,在 value2compare 第一次出现时修剪它,或者将第二个条件更改为:

If String.IsNullOrEmpty(value2compare.trim()) Then

并且你应该很好。

编辑:

基于澄清情况的解决方案

您希望将空字符串传递到扩展方法中以产生 True,对吗?另外,我更新了扩展方法,以允许更简洁的代码。但关键是您希望传入的任何空白字符串都会导致返回 True:

refinedresult = From x In theresult _
            Where x.<thelastname>.MatchesOrIsBlank(LastName) And _
                x.<thefirstname>.MatchesOrIsBlank(FirstName) And _
                x.<id>.MatchesOrIsBlank(Id) And _
                x.<number>.MatchesOrIsBlank(Telephone) And _
                x.<location>.MatchesOrIsBlank(Location) And _
                x.<building>.MatchesOrIsBlank(Building) And _
                x.<department>.MatchesOrIsBlank(Department) _
            Select x

并且:

Public Function TestPhoneElement(ByVal xE As XElement, ByVal value2compare As String) As Boolean
    Return xE.Value.ToLower.StartsWith(value2compare.ToLower.Trim)
End Function

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:

  1. Only ret = parent.ToLower.StartsWith(value2compare.ToLower.Trim) could possibly return true.
  2. value2compare.ToLower.Trim() would certainly cause the problem you indicate.
  3. 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 line value2compare.ToLower.Trim evaluates to an empty string because you trimmed it, and ret = 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:

If String.IsNullOrEmpty(value2compare.trim()) Then

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:

refinedresult = From x In theresult _
            Where x.<thelastname>.MatchesOrIsBlank(LastName) And _
                x.<thefirstname>.MatchesOrIsBlank(FirstName) And _
                x.<id>.MatchesOrIsBlank(Id) And _
                x.<number>.MatchesOrIsBlank(Telephone) And _
                x.<location>.MatchesOrIsBlank(Location) And _
                x.<building>.MatchesOrIsBlank(Building) And _
                x.<department>.MatchesOrIsBlank(Department) _
            Select x

And:

Public Function TestPhoneElement(ByVal xE As XElement, ByVal value2compare As String) As Boolean
    Return xE.Value.ToLower.StartsWith(value2compare.ToLower.Trim)
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文