【问题标题】:Add filter using XML使用 XML 添加过滤器
【发布时间】: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 =&gt; 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


【解决方案1】:

看看你的方法会很简单:

public static string GetChartEnergy(string initDate, string endDate, string type,string currentUser)
    {
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));

structure.Add(curentUser.CreateQueryStructure(string.Empty, false, string.Empty, "m.User", string.Empty, false));
}

假设 m.User 是要过滤的用户列 这会给你这样的输出

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) AND m.User IN("yourusername")

【讨论】:

  • @Dawin,一旦你运行了你的代码,请告诉我,如果你需要修改,请告诉我
  • 当然,我明天测试它,如果它有效,我会建议你。万分感谢 !问候
  • 你在那儿脏吗?
  • 是的,它有效,我用正确的解决方案编辑你的答案
  • @Dawin,我已经更新了答案。可能是适合您的新答案
猜你喜欢
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 2022-01-14
  • 2011-02-04
相关资源
最近更新 更多