使用通配符使用的LinqDataSource [英] Using wildcards with a LinqDataSource

查看:513
本文介绍了使用通配符使用的LinqDataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有被用作数据源的一个FormView ASP.NET页面上的LinqDataSource。我需要动态改变的基础上,可通过查询字符串传递的参数,其中条款。我有这个工作正常,只是我希望最终用户能够使用通配符:

I currently have a LinqDataSource on an ASP.NET page which is used as the data source for a FormView. I need to dynamically alter the where clause based on parameters that are passed through the query string. I have this working fine except that I want the end user to be able to use wildcards:

  • 在一个?用于重新present单个字符。
  • 在*号重presents多个字符。

似乎很容易,我需要做的使用LIKE运算符和替换 _ (匹配单个字符),而 * (匹配所有字符)。

Seems easy enough, all I need to do use the LIKE operator and replace ? with _ (match a single character), and * with % (match all characters).

下面是不花哨脚法(即所谓的 pageLoad的)的方法,它只是我的通配符转义的伟大工程。

Below is the method that does the fancy footwork (which is called on PageLoad), and it works great except that my wildcards are being escaped.

private void ApplyFilter(ref LinqDataSource lds)
{
    if (Request.QueryString.Keys.Count > 0)
    {
        string where = "";
        int counter = 0;

        foreach (string key in Request.QueryString.Keys)
        {
            if (Request.QueryString[key] != "")
            {
                if (counter == 0)
                {
                    where += key + ".Contains(@" + key + ")";
                }
                else
                {
                    where += " AND " + key + ".Contains(@" + key + ")";
                }

                lds.WhereParameters.Add(key, Request.QueryString[key].Replace("?", "_").Replace("*", "%"));
                counter++;
            }
        }

        if (where != "")
            lds.Where = where;
    }
}

下面是从该查询字符串生成的查询:<?code>的.aspx LOOP_DESCRIPTION = *&放大器; LOOP = *放大器;面积= 01

exec sp_executesql N'SELECT [t1].[AREA], [t1].[LOOP], [t1].[LOOP DESCRIPTION] AS [LOOP_DESCRIPTION]
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[AREA], [t0].[LOOP], [t0].[LOOP DESCRIPTION]) AS [ROW_NUMBER], [t0].[AREA], [t0].[LOOP], [t0].[LOOP DESCRIPTION]
    FROM [dbo].[INSTRUMENT LOOP DESCRIPTION] AS [t0]
    WHERE ([t0].[LOOP DESCRIPTION] LIKE @p0 ESCAPE ''~'') AND ([t0].[LOOP] LIKE @p1 ESCAPE ''~'') AND ([t0].[AREA] LIKE @p2)
    ) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p3 + 1 AND @p3 + @p4
ORDER BY [t1].[ROW_NUMBER]',N'@p0 nvarchar(4000),@p1 nvarchar(4000),@p2 nvarchar(4000),@p3 int,@p4 int',@p0=N'%~%%',@p1=N'%~%%',@p2=N'%01%',@p3=0,@p4=1

你可以通过参数赋值看,我的通配符被逃脱了波形符:

As you can see by the parameter assignment, my wildcards are being escaped with the tilde character:

@p0=N'%~%%',@p1=N'%~%%',@p2=N'%01%'

我的问题是,是否有可能停止 _ 被转义字符?

推荐答案

我终于设法得到这个想通了。由于从这个帖子的一类,我可以使用LinqDataSource_Selecting事件和过滤器我成功的数据使用通配符使用此code工作:

I have finally managed to get this figured out. Thanks to a class from this post, I am able to to use the LinqDataSource_Selecting event and filter my data successfully with wildcards working using this code:

protected void ldsData_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    var result = db.INSTRUMENT_LOOP_DESCRIPTIONs.AsQueryable();

    foreach (string key in Request.QueryString.Keys)
    {
        if (Request.QueryString[key].Trim() != "")
        {
            result = result.WhereLike(key, Request.QueryString[key].Replace("?", "_").Replace("*", "%"));
        }
    }

    e.Result = result;
}

这篇关于使用通配符使用的LinqDataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆