Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Unanswered

OData Query Replacing Characters

Posted on by Microsoft Employee

Hi, I am new to using OData querying and have been tasked with replacing database calls with our new REST web API using HTTP Web Requests.

I am using OData querying to do a search based upon the text the user wishes to search from. At the moment I have:

public static async Task<List<HorseJson>> SearchForHorses(string textSearch, HorseRacingClient gsaClient)
    {
        textSearch = textSearch.Replace("''", "");
        textSearch = textSearch.Replace(".", "");
        textSearch = textSearch.Replace(" ", "");
        textSearch = textSearch.Replace("-", "");
        textSearch = textSearch.Replace("(", "");
        textSearch = textSearch.Replace(")", "");
        textSearch = textSearch.Replace("*", "");
        textSearch = textSearch.Replace("\"", "");

        string query = string.Format("contains(horseName,{0})&$top=50&$orderby=horseName", textSearch);
        List<HorseJson> horseData = await gsaClient.SendODataRequest<HorseJson>("horses?$filter=" + query);
        return horseData;
    }


As you can see I am stripping out invalid characters from the textSearch.

I am querying the 'horseName' column field, but I need to strip the invalid characters out from that too so we can get a similar match.

This is possible to do in LINQ somewhat by doing something like:

textSearch = textSearch.Replace("-", "");
db.Horses.Where(x => x.HorseName.Replace("-","").Contains(textSearch)).ToList();

Is this possible for OData?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans