Query by dynamic properties

Applies To:yes OData AspNet WebApi V7yes OData AspNet WebApi V6

Since Web API OData V5.5, it supports filter, select and orderby on dynamic properties.

Let's see a sample about this feature.

CLR Model

First of all, we create the following CLR classes as our model:

public class SimpleOpenCustomer
{
    [Key]
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Website { get; set; }
    public IDictionary<string, object> CustomerProperties { get; set; }
}

Build Edm Model

Now, we can build the Edm Model as:

private static IEdmModel GetEdmModel()
{ 
    ODataModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<SimpleOpenCustomer>("SimpleOpenCustomers");
    return builder.GetEdmModel();
}

Use filter, orderby, select on dynamic property

Routing

In the SimpleOpenCustomersController, add the following method:

[EnableQuery]
public IQueryable<SimpleOpenCustomer> Get()
{
    return CreateCustomers().AsQueryable();
}

Request Samples

We can query like:

~/odata/SimpleOpenCustomers?$orderby=Token desc&$filter=Token ne null
~/odata/SimpleOpenCustomers?$select=Token