EntityDataSource replace * with % wildcard on queries

让人想犯罪 __ 提交于 2019-12-23 19:27:14

问题


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?


回答1:


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.




回答2:


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> 


来源:https://stackoverflow.com/questions/4632512/entitydatasource-replace-with-wildcard-on-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!