我正在使用动态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的错误:
“DateTime”类型中没有适用的方法'Parse'
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());
}
答案 0 :(得分:0)
我目前正在使用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对象作为字符串处理的需要。