【发布时间】:2017-03-14 02:33:32
【问题描述】:
我有这样的方法和查询:
public static string GetChartEnergy(string initDate, string endDate, string type)
{
structure.Add(initDate.CreateQueryStructure(endDate, true, null, "convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103)", null, false));
structure.Add(type.CreateQueryStructure(string.Empty, false, "CASE WHEN m.Type = 1 THEN 'Agua' ELSE CASE WHEN m.Type = 2 THEN 'Luz' ELSE 'Gas' END END AS Type", " m.type", "m.Type", false));
}
CreateQueryStructure 是这个:
public static QueryStructure CreateQueryStructure(this String value, string endDate, bool isDate,
string columnName, string whereName, string groupByName, bool isNullField)
{
QueryStructure structure = new QueryStructure();
if (!string.IsNullOrEmpty(value))
{
if (value != ",")
{
if (isDate)
{
//obtiene la estructura para un filtro entre fechas
structure.ColumnSelect = columnName;
structure.ColumnGroupBy = groupByName;
structure.ColumnWhere = string.Format("({0} BETWEEN convert(datetime,\'{1}\', 103) and convert(datetime,\'{2}\', 103))", whereName, value.Remove(value.Length - 1), endDate.Remove(value.Length - 1));
structure.Values = null;
structure.Operator = Operator.Nothing;
}
else
{
if (isNullField)
{
//obtiene la estructura de un filtro por un campo que es null o no
if (value.Remove(value.Length - 1) != "-1")
{
structure.ColumnWhere = string.Format("{0} IS{1} NULL", whereName,
value.Remove(value.Length - 1) == "0"
? " NOT" :
string.Empty);
structure.Values = null;
structure.Operator = Operator.And;
}
}
else
{
//obtiene la estructura de un campo aplicando la regla IN seleccionando
//el campo a mostrar y el campo en groupBy
structure.ColumnSelect = columnName;
structure.ColumnGroupBy = groupByName;
structure.ColumnWhere = whereName;
structure.Values = value.Remove(value.Length - 1);
structure.Operator = Operator.And;
}
}
}
}
return structure;
}
输出:"(convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103) BETWEEN convert(datetime,'01/01/2014', 103) and convert(datetime,'31/10/2016', 103))"
现在,当它从两个 CreateQueryStructure 传递时,我有另一种方法,例如:
public static string GetChartInfo(List<QueryStructure> queryStructure, string procedureName)
{
var queryWhere = queryStructure.GetWhere();
}
所以现在它传递给GetWhere:
public static string GetWhere(this List<QueryStructure> filters)
{
string result = string.Empty;
if (filters != null && filters.Count > 0)
{
if (filters.Select(x => x.ColumnWhere).Any())
{
result += "WHERE ";
foreach (var filter in filters)
{
if (filter.Operator != Operator.Nothing)
{
result += " " + filter.Operator.ToString() + " ";
}
if (!string.IsNullOrEmpty(filter.Values))
{
result += filter.ColumnWhere + " IN (";
result += filter.Values;
result += ") ";
}
else
{
result += filter.ColumnWhere;
}
}
}
}
return result;
}
最后它返回带有 where 子句的输出:
"WHERE (convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103) BETWEEN convert(datetime,'01/01/2014', 103) and convert(datetime,'31/10/2016', 103)) And m.type IN (2) "
我想更改 where 子句以仅获取 currentUser 的项目,因此我在从控制器发送参数的方法中获取 currentUser,例如:
public static string GetChartEnergy(string initDate, string endDate, string type, int currentUser) //there I have currentUser
现在,如何将此过滤器添加到:
structure.Add(initDate.CreateQueryStructure(endDate, true, null, "convert(datetime,'15/' + CONVERT(varchar(10), k.Month) + '/' + CONVERT(varchar(10), k.Year), 103)", null, false));
在 linq 中,我只需要像 x => x.user == currentUser 那样做一些事情,但在 xml 中我不知道怎么做,有人可以帮助我吗?问候
更新:查询结构类:
public class QueryStructure
{
public string ColumnGroupBy { get; set; }
public string ColumnSelect { get; set; }
public string ColumnWhere { get; set; }
public Operator Operator { get; set; }
public string Values { get; set; }
}
【问题讨论】:
-
您能否详细说明上述函数的输入与输出是什么,如果您粘贴 QueryStructure 类会很好。
-
我重组了我的问题以更清楚@DirtyDeveloper
-
能否请您也粘贴 QueryStructure 模型类
-
有@DirtyDeveloper
-
你在吗? @DirtyDeveloper
标签: c# json xml tsql sql-server-2012