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.
Unfortunately, activation email could not send to your email. Please try again.

How to filter enum types - ODataV4

Thread ID:

Created:

Updated:

Platform:

Replies:

132239 Aug 22,2017 10:22 AM Aug 25,2017 06:14 AM ASP.NET MVC 3
loading
Tags: Grid
Gregor
Asked On August 22, 2017 10:24 AM

Hello,

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 08:44 AM

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.  
 
  
  
[WebApiConfig.cs]  
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);  
            config.EnsureInitialized();  
  
        }  
  
  
 
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  
  
  
Regards,  
Manisankar Durai. 



Gregor
Replied On August 24, 2017 02:49 AM

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 06:14 AM

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.  
 
 
Regards, 
Manisankar Durai. 


CONFIRMATION

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.

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.

;