如何将Telerik网格过滤器表达式转换为SQL查询?

时间:2015-05-02 10:05:15

标签: parsing telerik-grid telerik-mvc

我在ASP.NET MVC 3项目中使用具有过滤功能的Telerik网格组件。我需要将过滤器表达式转换为从网格到SQL子句。 例如:" ID~eq~1"会变成" WHERE ID = 1"。

有没有内置方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

我使用此方法处理嵌套的AND和OR条件:

public static string GridFiltersToSQL(string filterBy)
{
    if (string.IsNullOrEmpty(filterBy) || filterBy == "~")
        return "";


    string filtersOut = "";

    var filtersAnd = filterBy.Split(new string[] { "~and~" }, StringSplitOptions.None);

    for (int i = 0; i < filtersAnd.Length; i++)
    {
        var filtersAndOr = new List<string>();

        if (filtersAnd[i].Contains("~or~"))
        {
            filtersAndOr = filtersAnd[i].Split(new string[] { "~or~" }, StringSplitOptions.None).ToList();
        }
        else
        {
            filtersAndOr.Add(filtersAnd[i]);
        }

        for (int k = 0; k < filtersAndOr.Count; k++)
        {
            filtersAndOr[k] = filtersAndOr[k].Replace("datetime", "");
            filtersAndOr[k] = filtersAndOr[k].Replace("T00-00-00", "");

            if (filtersAndOr[k].Contains("~eq~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~eq~true", " = 1");
                filtersAndOr[k] = filtersAndOr[k].Replace("~eq~false", " = 0");

                filtersAndOr[k] = filtersAndOr[k].Replace("~eq~", " = ");
            }
            else if (filtersAndOr[k].Contains("~ne~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~ne~", " <> ");
            }
            else if (filtersAndOr[k].Contains("~startswith~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~startswith~", " LIKE ");

                var j = filtersAndOr[k].LastIndexOf("'");
                if (j != -1)
                {
                    filtersAndOr[k] = filtersAndOr[k].Insert(j, "%");
                }
            }
            else if (filtersAndOr[k].Contains("~substringof~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~substringof~", " LIKE ");

                var j = filtersAndOr[k].IndexOf("'");
                if (j != -1)
                {
                    filtersAndOr[k] = filtersAndOr[k].Insert(j + 1, "%");
                }

                j = filtersAndOr[k].LastIndexOf("'");
                if (j != -1)
                {
                    filtersAndOr[k] = filtersAndOr[k].Insert(j, "%");
                }
            }
            else if (filtersAndOr[k].Contains("~notsubstringof~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~notsubstringof~", " NOT LIKE ");

                var j = filtersAndOr[k].IndexOf("'");
                if (j != -1)
                {
                    filtersAndOr[k] = filtersAndOr[k].Insert(j + 1, "%");
                }

                j = filtersAndOr[k].LastIndexOf("'");
                if (j != -1)
                {
                    filtersAndOr[k] = filtersAndOr[k].Insert(j, "%");
                }
            }
            else if (filtersAndOr[k].Contains("~endswith~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~endswith~", " LIKE ");

                var j = filtersAndOr[k].IndexOf("'");
                if (j != -1)
                {
                    filtersAndOr[k] = filtersAndOr[k].Insert(j + 1, "%");
                }
            }
            else if (filtersAndOr[k].Contains("~lt~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~lt~", " < ");
            }
            else if (filtersAndOr[k].Contains("~le~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~le~", " <= ");
            }
            else if (filtersAndOr[k].Contains("~gt~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~gt~", " > ");
            }
            else if (filtersAndOr[k].Contains("~ge~"))
            {
                filtersAndOr[k] = filtersAndOr[k].Replace("~ge~", " >= ");
            }
        }

        filtersAnd[i] = string.Join(" OR ", filtersAndOr);
    }

    filtersOut = string.Join(" AND ", filtersAnd);

    return filtersOut;
}