EntityDataSource 在查询中将 * 替换为 % 通配符

发布于 2024-10-11 10:02:54 字数 1020 浏览 1 评论 0原文

我有一个在很多地方使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-10-18 10:02:54

您可以处理页面回发并修改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.

也只是曾经 2024-10-18 10:02:54

Sluama - 你的建议解决了它!如此显而易见的答案。 " 正在终止Where 子句字符串。我可以发誓我尝试过,但我想没有。因为,我只是碰巧回到这个问题并看到你的答案,它有效!

<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> 

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!

<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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文