EntityDataSource 在查询中将 * 替换为 % 通配符
我有一个在很多地方使用 EntityDataSource 的应用程序。
在 EDS 中,我根据 TextBox 中的用户输入手动构建Where 子句。
我希望用户在查询数据时能够输入“*”(星号)而不是“%”。
有没有像使用 Entity SQL 或 EDS 本身一样简单的方法来进行搜索/替换?我知道我实际上可以在输入数据后更改文本框,但是当用户看到他的文本从 * 更改为 % 我认为他不会理解。
我尝试使用 T-SQL Replace 命令并执行以下操作:
<asp:EntityDataSource ID="EDSParts" runat="server"
ConnectionString="name=TTEntities" DefaultContainerName="TTEntities"
EnableFlattening="False" EntitySetName="Parts"
OrderBy="it.ID DESC"
Where ="(CASE
WHEN (@PartNumber IS NOT NULL) THEN
it.[Number] LIKE REPLACE(@PartNumber, "*", "%")
ELSE
it.[ID] IS NOT NULL
END)">
<WhereParameters>
<asp:ControlParameter Name="PartNumber" Type="String"
ControlID="txtPartNumberQuery" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>
但我收到“服务器标记格式不正确”消息。我在 Entity SQL 参考中找不到等效的“替换”函数......
有什么想法吗?
I have an application that uses EntityDataSource in many places.
In the EDS, I manually build the Where clause based on user input from TextBox'es.
I would like the user to be able to enter "*" (asterisks) instead of "%" when querying data.
Is there an easy as using Entity SQL or the EDS itself to do a search/replace? I know I could actually change the TextBox after the data is entered, but when the user sees his text was changed from an * to a % I don't think he will understand.
I have tried using the T-SQL Replace command and doing something like this:
<asp:EntityDataSource ID="EDSParts" runat="server"
ConnectionString="name=TTEntities" DefaultContainerName="TTEntities"
EnableFlattening="False" EntitySetName="Parts"
OrderBy="it.ID DESC"
Where ="(CASE
WHEN (@PartNumber IS NOT NULL) THEN
it.[Number] LIKE REPLACE(@PartNumber, "*", "%")
ELSE
it.[ID] IS NOT NULL
END)">
<WhereParameters>
<asp:ControlParameter Name="PartNumber" Type="String"
ControlID="txtPartNumberQuery" PropertyName="Text" />
</WhereParameters>
</asp:EntityDataSource>
But I get a "Server tag is not well formed" message. I can't find an equivalent "replace" function in the Entity SQL reference....
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以处理页面回发并修改txtPartNumberQuery 的内容。 EntityDataSource 只能与 % 一起使用(因为它构建 ESQL 查询),因此在执行数据绑定之前,您必须在代码隐藏中将 * 更改为 %。
You can handle page postback and modify content of txtPartNumberQuery. EntityDataSource can work only with % (because it builds ESQL query) so you have to change * to % in your codebehind before you execute databinding.
Sluama - 你的建议解决了它!如此显而易见的答案。 " 正在终止Where 子句字符串。我可以发誓我尝试过,但我想没有。因为,我只是碰巧回到这个问题并看到你的答案,它有效!
Sluama - Your suggestion fixed it! Such an obvious answer. The " was terminating the Where clause string. I could have sworn I tried that, but I guess not. Becuase, I just happened to come back to this question and saw your answer and it works!