Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Azure search for text containing quotes(escape single quote)?

I have a Query for azure search as given below

results = indexClient.Documents.Search<Hotel>("", new SearchParameters { IncludeTotalResultCount = true, Filter = "(Provider eq 'Auction.com' or Provider eq 'Zeroiron' or Provider eq 'Gilbert'sYard')" });

the current Query Gives error because as i have given every provider inside quotes , but Gilbert's Yard already have a Quote inside the provider name itself, so to search for same Query with "Gilbert's yard" what change i have to make in the Query?

The above Query is generated like this,

        var selectedProviders = this.Providers.Where(i => i.IsSearchable).ToList();
            if (selectedProviders.Count > 0)
            {
                if (filterString.Length > 0)
                    filterString.Append(" and ");
                filterString.Append("(");
                var count = 1;
                foreach (var provider in selectedProviders)
                {

                    filterString.Append(($"Provider eq '{provider.ProviderName}'"));

                    if (count < selectedProviders.Count)
                    {
                        filterString.Append(" or ");
                    }
                    count++;
                };
                filterString.Append(")");
            }

And how i should change my Code here?

like image 295
hilda_sonica_vish Avatar asked May 02 '17 05:05

hilda_sonica_vish


People also ask

How do I search for a single quote in SQL?

The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one. The single quote is the escape character in Oracle, SQL Server, MySQL, and PostgreSQL.


1 Answers

To achieve this we need to replace single quote with two single quotes.

filterString.Append(($"Provider eq '{provider.ProviderName.replace("'","''")}'"));

this will do the trick for me.

like image 62
hilda_sonica_vish Avatar answered Oct 29 '22 01:10

hilda_sonica_vish