We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to filter enum types - ODataV4

Thread ID:





132239 Aug 22,2017 02:22 PM UTC Aug 25,2017 10:14 AM UTC ASP.NET MVC 3
Tags: Grid
Asked On August 22, 2017 02:24 PM UTC


I'm using ASP.NET MVC and Syncfusion Grid Control with ODataV4 Api data source.

I have no idea how to filter my column with enum type, becouse grid creates wrong query, not proper for OData enum filtering.

For example:
My enum type is NotificationStatus (Project.Entities.NotificationStatus). If I use this api route "/api/Notifications" i get JSON response:

Id: "1FZT6PI2",
SenderName: "Mathew Johnson",
NotificationStatus: "New",
Subject: "I have problem",
Date: "2017-08-15T11:47:28+02:00"

If I would like filter my database using enum i should use this query:
 /api/Notifications/?$filter=NotificationStatus eq Project.Entities.NotificationStatus'New'

But i've noticed that columns types are: string, number, date, datetime, boolean, guid, checkbox. So no one of them is able to generate good query.
If I use string type it generates: $filter=tolower(TicketStatus) eq 'new' or $filter=contains(tolower(TicketStatus),'new')
If I use number type it generates: $filter=TicketStatus eq 1
But it is wrong for enum..

How can i generate proper query?

Or maybe I can edit generated query and add "Project.Entities.NotificationStatus" before the filter value. How to do it?

Thanks for help!

Mani Sankar Durai [Syncfusion]
Replied On August 23, 2017 12:44 PM UTC

Hi Gregor, 
Thanks for contacting Syncfusion support.  
We have checked the provided code and found that you are using the ODataV4. To filter the enum type value in ODataV4, please enable the enumPrefixFree in WebApi configuration. Refer to the sample and code example as below.  
public static void Register(HttpConfiguration config)  
            . . . .   
           // Create our batch handler and associate it with the OData service.  
            ODataBatchHandler batchHandler = new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer);  
            config.MapODataServiceRoute("odata", "odata", model, batchHandler);  
            config.EnableEnumPrefixFree(enumPrefixFree: true);  
Refer to the document for more information in following link.  
Custom Url parsing -> Enum prefix free:  http://odata.github.io/WebApi/#06-01-custom-url-parsing  
Manisankar Durai. 

Replied On August 24, 2017 06:49 AM UTC

Thank you very much for answer. This solution works! 

In your example code you used Microsoft.AspNet.OData version 5.5.1, I tried using this version too and it works. But when I use version 6.0.0 the method "EnableEnumPrefixFree()" is missing. I can't find why it is missing or maybe now is another way to free the prefixes...

Mani Sankar Durai [Syncfusion]
Replied On August 25, 2017 10:14 AM UTC

Hi Gregor,   
Thanks for your update.   
Please try the solution provided in the below given blog in your sample   
Replace EnableEnumPrefixFree by registering a StringAsEnumResolver in the configureAction of call to MapODataServiceRoute in webConfigApi.cs file.  
Manisankar Durai. 


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon