Web API OData V4 using enum with functions and Entities Part 4

This post is part 4 of the Web API and OData V4 series. This article shows how Web API 2.2 with OData V4 supports enum in entities or as return values and also supports enum parameters for functions.

Part 1 Getting started with Web API and OData V4 Part 1.
Part 2 Web API and OData V4 Queries, Functions and Attribute Routing Part 2
Part 3 Web API and OData V4 CRUD and Actions Part 3
Part 4 Web API OData V4 Using enum with Functions and Entities Part 4
Part 5 Web API OData V4 Using Unity IoC, SQLite with EF6 and OData Model Aliasing Part 5
Part 6 Web API OData V4 Using Contained Models Part 6
Part 7 Web API OData V4 Using a Singleton Part 7
Part 8 Web API OData V4 Using an OData T4 generated client Part 8
Part 9 Web API OData V4 Caching Part 9
Part 10 Web API OData V4 Batching Part 10
Part 11 Web API OData V4 Keys, Composite Keys and Functions Part 11

Code: https://github.com/damienbod/WebAPIODataV4

To demonstrate OData with enums, an PhoneNumberTypeEnum enum is created and also an entity class with the string property ‘Name’ as the OData key.

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace WebAppODataV4.Models
    public enum PhoneNumberTypeEnum
        Cell = 1,
        Home = 2,
        Work = 3

    public class EntityWithEnum
        public string Description { get; set; }
        public PhoneNumberTypeEnum PhoneNumberType { get; set; }

        public string Name { get; set; }

Using Entities which have enum Properties

The EntityWithEnum is added to the OData model builder.


An EntityWithEnumController which implements ODataController is used to make the OData entity set available in the service. Some dummy data has been added in the contructor.

public class EntityWithEnumController : ODataController
  private List<EntityWithEnum> someData = new List<EntityWithEnum>();

  public EntityWithEnumController()
    someData.Add(new EntityWithEnum { Description = "test1", Name = "Van", PhoneNumberType = PhoneNumberTypeEnum.Home});
    someData.Add(new EntityWithEnum { Description = "test2", Name = "Bill", PhoneNumberType = PhoneNumberTypeEnum.Work });
    someData.Add(new EntityWithEnum { Description = "test3", Name = "Rob", PhoneNumberType = PhoneNumberTypeEnum.Cell });

  [EnableQuery(PageSize = 20)]
  public IHttpActionResult Get()
    return Ok(someData);

  [EnableQuery(PageSize = 20)]
  public IHttpActionResult Get([FromODataUri] string key)
    if (someData.Exists(t => t.Name == key))
      return Ok(someData.FirstOrDefault(t => t.Name == key));

    return BadRequest("key does not key");

Now an entity can be requested with a string key.


The enum is serialized and returned in the Json object.


Using enums in OData Functions

OData functions support using enum as parameters or return values. To demonstrate this, a new OData function is created, which takes a PhoneNumberTypeEnum enum parameter and returns an enum parameter.

EntitySetConfiguration<EntityWithEnum> entitesWithEnum 
    = builder.EntitySet<EntityWithEnum>("EntityWithEnum");

FunctionConfiguration functionEntitesWithEnum 
    = entitesWithEnum.EntityType.Collection.Function("PersonSearchPerPhoneType");


The function method is added to the ODataController.

[EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
public IHttpActionResult PersonSearchPerPhoneType([FromODataUri] PhoneNumberTypeEnum phoneNumberTypeEnum)
  if (!ModelState.IsValid)
    return BadRequest(ModelState);

  return Ok(someData.Where(t => t.PhoneNumberType.Equals(phoneNumberTypeEnum)));

The function can be called with a Http Get request. The enum is entered as a parameter; the enum property with full namespace.


The request returns a Json object with the enum value.


Note: At present an enum cannot be used as a parameter for an action.





SAMPLES: https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/













  1. ibrahim · · Reply

    Thank you very much!
    But how can we return enum’s integer value, not string?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: