0

在我的场景中,我必须根据用户在屏幕上选择的内容构建动态查询。

用户可以选择一个列名,然后选择一个运算符,然后键入一个值。

我已经这样做了,但是 LIKE 和 NOT LIKE 的语法如何呢?所以

第一个。我将列名添加到我的列表中

var columns = new Dictionary<string, string>
              {
                {"CurrentStatus", "Current Status"},
                {"RequestNumber", "Request Number"},
                {"RequestDate", "Request Date"},
                {"IsOnHold", "Is On Hold"},
                {"BrandReturnedVehicle", "Brand Returned Vehicle"},
                {"TypeReturnedVehicle", "Type Returned Vehicle"},
                {"ChassisReturnedVehicle", "Chassis Returned Vehicle"},
                {"DestructionCertificateNumberReturnedVehicle", 
                              "Destruction Certificate Number Returned Vehicle"},
                {"AmmountWithVAT", "Ammount WithVAT "},
                {"AmmountWithoutVat", "Ammount Without Vat"},
                {"Percentage", "Percentage"},
                {"VehicleDestructionDate", "Vehicle Destruction Date"},
                {"Comments", "Comments"},
                {"Discriminator", "Request Type"},
              };

                DdlColumn1.DataSource = columns;  
                DdlColumn1.DataTextField = "Value";
                DdlColumn1.DataValueField = "Key";              
                DdlColumn1.DataBind();

第二。根据所选的列名称,我将运算符添加到下拉列表中。

protected void DdlColumn1SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadOperatorsDependingOnColumn(sender as DropDownList, DdlColumn1.SelectedValue);
        }


    private void LoadOperatorsDependingOnColumn(DropDownList ddlOperators, string columnname)
                {
                    var operators = new Dictionary<string, string>();
                    operators.Clear();
                    switch (columnname)
                    {
                        case "CurrentStatus":
                            AddTextOperatorsToList(operators);
                            ddlOperators.DataSource = operators;
                            ddlOperators.DataTextField = "Value";
                            ddlOperators.DataValueField = "Key";
                            ddlOperators.DataBind();
                            break;
                        case "AmmountWithVat":
                            AddNumberOperatorsToList(operators);
                            break;
                    }
                }


private static void AddTextOperatorsToList(Dictionary<string, string> operators)
            {
                operators.Add("==", "Equals");
                operators.Add("<>", "Not Equals");
                operators.Add("LIKE", "Contains");
                operators.Add("NOT LIKE", "Does not Contain");
            }

            private static void AddNumberOperatorsToList(Dictionary<string, string> operators)
            {
                operators.Add("=", "Equals");
                operators.Add("<>", "Not Equals");
                operators.Add(">", "Greater than");
                operators.Add(">=", "Greater or equal than");
                operators.Add("<", "Less than");
                operators.Add("<=", "Less or equal than");
            }

private string ColumnType(string columnName)
            {
                switch (columnName)
                {
                    case "CurrentStatus":
                        return "Text";
                        break;
                    case "RequestNumber":
                        return "Text";
                        break;
                }
            }

            private string BuildQuery()
            {
                var sb = new StringBuilder();
                        //var list = RequestBaseBL.GetRequestByCustomQuery("RequestNumber == \"12\"");

                if (ColumnType(DdlColumn1.SelectedValue) == "Text" && DdlOperator1.SelectedItem.Text=="==")
                {
                    sb.Append(DdlColumn1.SelectedValue);
                    sb.Append(DdlOperator1.SelectedValue);
                    sb.Append("\"" +  TxtValue1.Text + "\"");
                }
  1. 我不知道如何连接/附加字符串以使不等于、包含和不包含与动态 linq 库一起使用
4

1 回答 1

2

看起来动态 linq 的东西不支持 'LIKE' - 但我想这就是你问这个问题的原因。我能想到的最好的办法是用 (x >= y0 AND x < y1) 之类的东西替换 LIKE。
所以:

if (ColumnType(DdlColumn1.SelectedValue) == "Text" && DdlOperator1.SelectedItem.Text=="LIKE")
{
    string s = TxtValue1.Text;
    Char c = s[s.Length - 1];
    string s1 = s.Substring(0, s.Length - 1) + ((Char)(c + 1));
    string clause = string.Format("{0} >= \"{1}\" and {0} < \"{2}\"", DdlColumn1.SelectedValue, s, s1);
    sb.Append(clause);
}

即,将搜索字符串的最后一个字符的值加 1,并将其用作搜索的上限。
如果你知道你只处理简单的拉丁字符集,你可以让它稍微简单一些并使用:

string clause = string.Format("{0} >= \"{1}\" and {0} <= \"{1}z\"", DdlColumn1.SelectedValue, TxtValue1.Text);

但是可能值得看看这里的谓词构建器以获得更多的类型安全性。

编辑

嗯,我从来没有!忘记这一切。

看来您可以将“myField.Contains(myCriteria)”和“myField.StartsWith(myCriteria)”用于 CONTAINS 和 LIKE

sb.Append(string.Format("{0}.Contains(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text);

sb.Append(string.Format("{0}.StartsWith(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text);

并且,对于不喜欢:

sb.Append(string.Format("!{0}.StartsWith(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text);
于 2012-05-31T08:25:38.333 回答