在我的场景中,我必须根据用户在屏幕上选择的内容构建动态查询。
用户可以选择一个列名,然后选择一个运算符,然后键入一个值。
我已经这样做了,但是 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 + "\"");
}
- 我不知道如何连接/附加字符串以使不等于、包含和不包含与动态 linq 库一起使用