简体   繁体   中英

Dynamic linq, how to generate LIKE and NOT LIKE

In my scenario I have to build a dynamic query based on what the user selects on the screen.

The user can select a columnname, then an operator and then type a value.

I already did it for the equal, but how it would be the syntax for the LIKE and NOT LIKE? so

1st. I add the Column names to my list

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();

2nd. Depending on the selected column name, I add the operators to the dropdownlist.

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. What I dont know is how to concatenate/append the strings to make the Not Equal, Contains and Does not contain to work with the dynamic linq libray

It would appear that the dynamic linq stuff doesn't support 'LIKE' - but I guess that's why you're asking the question. The best I could come up with is to replace LIKE with something like (x >= y0 AND x < y1).
So:

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);
}

ie Add 1 to the value of the last character of the search string and use that as an upper bound for the search.
If you know you're only dealing with simple Latin charactersets you could make it slightly simpler and use:

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

But it might be worth taking a look at the predicate builder here to get more type safety.

EDIT

Well I never! Forget all that.

It seems you can use "myField.Contains(myCriteria)" and "myField.StartsWith(myCriteria)" for CONTAINS and LIKE

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

and

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

and, for NOT LIKE:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM