1

现在,我正在通过一堆 if 语句构建一个基于用户输入(文本框和组合框)的过滤器。肯定有更好的办法。这是我当前的代码:

    Private Function BuildProductsFilter() As String

        Dim RawFilterResults As String = ""

        If Not CompanyNameComboBox.SelectedValue Is Nothing AndAlso Not CompanyNameComboBox.SelectedValue.ToString = "[ALL]" Then
            RawFilterResults = "companyname = '" & CompanyNameComboBox.SelectedValue.ToString & "'"
        End If

        If QtyTextbox.Text > "" AndAlso IsNumeric(QtyTextbox.Text) Then
            RawFilterResults &= " and stock = " & QtyTextbox.Text
        End If

        If KeywordTextbox.Text > "" Then
            RawFilterResults &= " and (description like '%" & KeywordTextbox.Text & "%'"
            RawFilterResults &= "or descriptionlong like '%" & KeywordTextbox.Text & "%'"
            RawFilterResults &= "or details like '%" & KeywordTextbox.Text & "%')"
        End If

        If SKUTextbox.Text > "" Then

            If SKUTextbox.Text.StartsWith("*") Then
                RawFilterResults &= " and sku like '%" & SKUTextbox.Text & "'"
            ElseIf SKUTextbox.Text.EndsWith("*") Then
                RawFilterResults &= " and sku like '" & SKUTextbox.Text & "%'"
            Else
                RawFilterResults &= " and sku = '" & SKUTextbox.Text & "'"
            End If

        End If

        If Not AllowPurchaseCombobox.SelectedItem Is Nothing AndAlso Not AllowPurchaseCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and allowpurchase = '" & AllowPurchaseCombobox.SelectedValue.ToString & "'"
        End If

        If Not ShowPriceCombobox.SelectedItem Is Nothing AndAlso Not ShowPriceCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and showprice = '" & ShowPriceCombobox.SelectedValue.ToString & "'"
        End If

        If Not VirtualLoupeCombobox.SelectedItem Is Nothing AndAlso Not VirtualLoupeCombobox.SelectedItem.ToString = "[ALL]" Then
            RawFilterResults &= " and VirtualLoupe = '" & VirtualLoupeCombobox.SelectedValue.ToString & "'"
        End If

        If ImageTextbox.Text > "" Then
            Dim ImageDir As String = Path.GetDirectoryName(ImageTextbox.Text)
            RawFilterResults &= " and imageurl like '" & ImageDir & "%'"
        End If

        If CaratTextbox.Text > "" Then
            RawFilterResults &= " and carat = '" & CaratTextbox.Text & "'"
        End If

        If CutTextbox.Text > "" Then
            RawFilterResults &= " and cut = '" & CutTextbox.Text & "'"
        End If

        If ColorTextbox.Text > "" Then
            RawFilterResults &= " and color = '" & ColorTextbox.Text & "'"
        End If

        If ClarityTextbox.Text > "" Then
            RawFilterResults &= " and Clarity = '" & ClarityTextbox.Text & "'"
        End If

        If RawFilterResults.StartsWith(" and ") Then
            RawFilterResults = RawFilterResults.Substring(4)
        End If

        Return RawFilterResults

    End Function
4

1 回答 1

1

使用流畅的风格界面。简单示例在这里

或者更好的是使用 ORM,这样你就没有字符串编码的字段名称等

于 2009-12-31T20:55:34.733 回答