[英]Using wildcards with a LinqDataSource
I currently have a LinqDataSource on an ASP.NET page which is used as the data source for a FormView. 我目前在ASP.NET页上有一个LinqDataSource,它用作FormView的数据源。 I need to dynamically alter the where
clause based on parameters that are passed through the query string. 我需要根据通过查询字符串传递的参数动态更改where
子句。 I have this working fine except that I want the end user to be able to use wildcards: 除了我希望最终用户能够使用通配符之外,我的工作还不错:
Seems easy enough, all I need to do use the LIKE operator and replace ?
似乎很容易,我需要使用LIKE运算符进行替换?
with _
(match a single character), and *
with %
(match all characters). 与_
(匹配单个字符),和*
与%
(匹配所有字符)。
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. 下面是执行花式步法的方法(在PageLoad
上调用该方法),它的工作原理非常好,除了我的通配符已被转义。
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;
}
}
Below is the query that is generated from this query string: .aspx?LOOP_DESCRIPTION=*&LOOP=*&AREA=01
下面是从该查询字符串生成的查询: .aspx?LOOP_DESCRIPTION=*&LOOP=*&AREA=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%'
My question is, is it possible to stop the _
and %
characters from being escaped? 我的问题是,是否可以阻止_
和%
字符被转义?
Please try url decode Request.QueryString[key]
请尝试url解码Request.QueryString[key]
Example: 例:
HttpUtility.UrlDecode(Request.QueryString[key]);
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: 由于从类这篇文章 ,我能够使用LinqDataSource_Selecting事件,并使用此代码工作通配符成功筛选我的数据:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.