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 に答える