Day of week Function in OData v4 Web Api

793 views Asked by At

It doesn't look like there's any built in function for getting the day of week using a function based on the documentation here:

http://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part2-url-conventions.html

What I want is a Web Api 2.2 OData V4 implementation that can service a url request like this:

/meeting?$filter=dayofweek(StartDate) eq 'Wednesday'

or something similar. But it seems like a pipe dream at this moment. Can someone show how this could be done? Would it have to be done using something like this?

  builder.EntityType<Meeting>().Collection
            .Function("DayOfWeek")
            .Returns<IEnumerable<Meeting>>();

then

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;

namespace Test.Controllers
{
    public class MeetingsController : ODataController
    {
        private EntityContext db = new EntityContext();

        [EnableQuery]
        public IQueryable<Meeting> GetMeetings()
        {
            return db.Meetings;
        }

        [HttpGet]
        public IHttpActionResult DayOfWeek(DateTime dayofweek)
        {
            //calculate day of week and return string
         }
    }
}
1

There are 1 answers

2
Yi Ding - MSFT On BEST ANSWER

You are correct about there being no build-in functions for getting day of week. From the protocol perspective, defining custom function to be used in query clauses should resolve the problem.

Unfortunately, although you can follow this blog post to define your own DayOfWeek as an unbound function, the current version of Web API OData V4 only supports such functions being called at the service root. Thus can't be used for your scenario.

The good news is, that such support of defining custom unbound function to be used in query clauses is prioritized for the release after the next. (next will be in December, the one after next will be in February/March). You can use it then.