【问题标题】:Linq Query ignore empty parametersLinq Query 忽略空参数
【发布时间】:2010-04-20 19:04:25
【问题描述】:

如何让 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

【问题讨论】:

    标签: .net vb.net linq linq-to-xml xelement


    【解决方案1】:

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

    我的想法是这样的:

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

    我相信您传递给TestPhoneElement 的第二个参数实际上必须是一个包含至少一个空格的字符串。这样,String.IsNullOrEmpty(value2compare)返回假。然后在最后一行 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
    

    【讨论】:

    • 我会试试的,谢谢你的回答。 +1 进行了如此详细的尝试,如果可行,我会报告。再次感谢。
    • 好的,我的代码出错了。我真的很想“与”每个参数。当我在鬼混试图让它工作时,我就在那里有了 OR。所以最后我目前没有得到任何结果。 (我更新了问题)我得到结果的唯一时间是所有参数是否都有一些东西。所以是的,它与任何参数中的空字符串有关。
    • 那么,在 value2compare 参数为空字符串(或什么都没有)的情况下,您希望 TestPhoneElement 返回 True 还是 False?
    • 也许我可以通过解释我试图用我的代码来完成什么来回答这个问题。甚至可能不需要扩展方法。我想传递查询搜索参数,但允许一些不传递。所以部门可能不会被传递给查询。现在,当且仅当所有参数都传递时,上面的代码才有效。坦率地说,我认为我的代码会按原样工作,但由于参数的空字符串而失败。这解释得很好吗?
    • 感谢您的更新。我实施了你的建议。我收到编译器错误。它说扩展方法不是 System.Collections.Generic.Ienumerable(Of System.XMl.Linq.Xelement) 的成员
    猜你喜欢
    • 2018-03-16
    • 2019-04-11
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    相关资源
    最近更新 更多