- Home
- Forum
- ASP.NET MVC
- How to filter enum types - ODataV4
How to filter enum types - ODataV4
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!
Thanks for help!
SIGN IN To post a reply.
3 Replies
MS
Mani Sankar Durai
Syncfusion Team
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.
Sample: https://www.syncfusion.com/downloads/support/forum/132239/ze/Sample-ODataV4-417100303_(1)755671043
| [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: https://odata.github.io/WebApi/#06-01-custom-url-parsing
Regards,
Manisankar Durai.
GR
Gregor
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...
MS
Mani Sankar Durai
Syncfusion Team
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.
Regards,
Manisankar Durai.
SIGN IN To post a reply.
- 3 Replies
- 2 Participants
-
GR Gregor
- Aug 22, 2017 02:22 PM UTC
- Aug 25, 2017 10:14 AM UTC