OData V4 + WebAPI Filter by Int Meaning Enum?

OData V4 has enumeration support, but it seems you only need to search by namespace. How now to search by value instead of text representation?

In V3 odata, you can request $filter=Status eq 35 , where 35 is Complete in the listing. This method would work even if this field was an enumeration field in the data model.

Now this method does not work in V4, instead a namespace with a textual representation of the enumeration is required.

I want V3 support to work again without losing other odata V4 features. A search by int value for an enum element seems more reliable than a text search. Older odata clients (such as kendo) do not support the filtering method of listing as text.

+6
source share
2 answers

to do this in OData v4, we can enable EnumPrefixFree in the initial webapi configuration, so we don’t need to write the full enum namespace as a prefix:

 public static void Register(HttpConfiguration config) { // ... config.EnableEnumPrefixFree(enumPrefixFree: true); config.MapODataServiceRoute("odata", "odata", YourEdmModem); // ... } 

then we can filter any enumeration by String or Int value:

 $filter=Status eq 'single' 

or

 $filter=Status eq 1 

hope this helps.

+13
source

With v4, do you need to add a namespace as a prefix and surround the value with a single quote, for example http://services.odata.org/V4/(S(m1bhpaebr1yvzx5vtz5v4ur1))/TripPinServiceRW/People ? $ filter = Gender % 20eq % 20Microsoft.OData.SampleService.Models.TripPin.PersonGender'1 ', where 1 represents Female.

Here is a quote from the ABNF protocol http://docs.oasis-open.org/odata/odata/v4.0/os/abnf/odata-abnf-construction-rules.txt :

 enum = qualifiedEnumTypeName SQUOTE enumValue SQUOTE enumValue = singleEnumValue *( COMMA singleEnumValue ) singleEnumValue = enumerationMember / enumMemberValue enumMemberValue = int64Value 
+9
source

Source: https://habr.com/ru/post/974318/


All Articles