2

我正在使用动态 linq 库构建动态查询构建器。到目前为止,它适用于布尔、数字和文本,但不适用于日期。

我想知道日期的格式应该是什么才能正常工作。

这是执行 sb.ToString 返回的代码。

private string BuildQuery()
        {
            var sb = new StringBuilder();
            //var list = RequestBaseBL.GetRequestByCustomQuery("RequestNumber == \"12\"");
        #region 1st ROW of the QUERY
        if (ColumnType(DdlColumn1.SelectedValue) == "Text")
        {
            if(DdlOperator1.SelectedValue == "==")
            {
                sb.Append(DdlColumn1.SelectedValue);
                sb.Append(DdlOperator1.SelectedValue);
                sb.Append("\"" + TxtValue1.Text + "\"");
            }
            if (DdlOperator1.SelectedValue == "<>")
            {
                sb.Append(DdlColumn1.SelectedValue);
                sb.Append(DdlOperator1.SelectedValue);
                sb.Append("\"" + TxtValue1.Text + "\"");
            }
            if (DdlOperator1.SelectedValue == "LIKE")
            {
                sb.Append(string.Format("{0}.Contains(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text));
            }
            if (DdlOperator1.SelectedValue == "NOT LIKE")
            {
                sb.Append(string.Format("!{0}.Contains(\"{1}\")", DdlColumn1.SelectedValue, TxtValue1.Text));
            }
        }

        if (ColumnType(DdlColumn1.SelectedValue) == "Number")
        {
            sb.Append(DdlColumn1.SelectedValue);
            sb.Append(DdlOperator1.SelectedValue);
            sb.Append(TxtValue1.Text);
        }

        if (ColumnType(DdlColumn1.SelectedValue) == "Date")
        {
            sb.Append(DdlColumn1.SelectedValue);
            sb.Append(DdlOperator1.SelectedValue);
            sb.Append(TxtValue1.Text);
        }

        if (ColumnType(DdlColumn1.SelectedValue) == "Bool")
        {
            sb.Append(DdlColumn1.SelectedValue);
            sb.Append(DdlOperator1.SelectedValue);
            sb.Append(TxtValue1.Text);
        }

        #endregion  


        return sb.ToString();
    }


  string strSql = BuildQuery();
            try
            {
                var list = RequestBaseBL.GetRequestByCustomQuery(strSql, DdlRequestType.SelectedValue).ToList();



private static void AddDateOperatorsToList(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");
            }

更新1:

错误是:运算符 '=' 与操作数类型 'DateTime' 和 'Int32' 不兼容

sb.toString()

? sb.ToString() "RequestDate=12/12/2015"

更新 2:

更新 2 的错误:

“日期时间”类型中不存在适用的方法“解析”

if (ColumnType(DdlColumn1.SelectedValue) == "Date")
                {
                    sb.AppendFormat("DateTime.Parse({0})", DdlColumn1.SelectedValue);
                    var str = string.Format("{0} {1} {2}", DdlColumn1.SelectedValue, DdlOperator1.SelectedValue,
                                               DateTime.Parse(TxtValue1.Text));
                    //sb.Append(DdlColumn1.SelectedValue);
                    //sb.Append(DdlOperator1.SelectedValue);
                    //sb.Append(TxtValue1.Text);
                    sb.Append(str);
                }

更新 3:

也没有工作

if (ColumnType(DdlColumn1.SelectedValue) == "Date")
                {
                    var date = DateTime.Parse(TxtValue1.Text);
                    sb.Append(DdlColumn1.SelectedValue);
                    sb.Append(DdlOperator1.SelectedValue);
                    sb.Append("\"" + date.ToUniversalTime() + "\"");
                    //sb.Append(date.ToUniversalTime());
                }

在此处输入图像描述

4

1 回答 1

1

我目前正在使用 System.Dynamic.Linq 并使用带有 DateTime 对象的双等号。

case "Equals":
    myDictionary.Add("MyField.Date ==", dt1);
    break;

后来我将字典中的键转换为字符串,将值转换为数组(注意@符号的用法):

if (myDictionary.Keys.Count > 0)
{
  var conditions = myDictionary.Keys.Select((key, idx) => string.Format("{0} (@{1})", key, idx));

  // in .NET 4, the ToArray() part can go away
  predicateString = string.Join(" and ", conditions.ToArray());
  predicateValues = myDictionary.Values.ToArray();
}

然后调用 AsQueryable().Where()

return myList.AsQueryable().Where(predicateString, predicateValues).ToList();

我相信这样做可以消除将 DateTime 对象作为字符串处理的需要。

于 2012-06-01T12:03:30.357 回答