Reputation: 35726
I've written a OData WebAPI controller inherting from ODataController
.
public class ManyColumnsController : ODataController
{
[Queryable(
AllowedOrderByProperties = "Aa,Bb,Cc,Dd",
EnsureStableOrdering = false,
MaxOrderByNodeCount = 2)]
public IQueryable<ManyColumn> GetManyColumns(
ODataQueryOptions<ManyColumn> options)
{
// Because I've disabled EnsureStableOrdering,
// I need to check column "Dd" is always included
// in the OrderBy option.
if (!options.OrderBy.RawValue.Contains("Dd")
{
// I need to add an OrderByPropertyNode to
// options.OrderBy.OrderByNodes.
}
return this.context.ManyColumns;
}
}
How can I instantiate an OrderByNode
to add to the generic collection exposed by OrderByQueryOption
?
You'll note that one constructor of OrderByNode
takes an IEdmProperty
. I feel there should me some trivial way to find an instance of IEdmProperty
from the IEdmModel
exposed at options.Context.Model
. and the IEdmType
at options.Context.ElementType
.
If this is achievable, I could use the instance in place of the string search in the if
statement. In addition to adding it to the whole OrderByClause
.
Upvotes: 2
Views: 1722
Reputation: 2995
You can get the property just as Jinfu said,
but I think you can new an OrderByQueryOption and call ApplyTo to meet your requirement.
var result = this.context.ManyColumns;
if (!options.OrderBy.RawValue.Contains("Dd")
{
var orderBy = new OrderByQueryOption("Dd", new ODataQueryContext(Request.GetEdmModel(), typeof(ManyColumn)));
result = orderBy.ApplyTo(result );
}
Upvotes: 1
Reputation: 3345
IEdmEntityType entityType=options.Context.ElementType as Microsoft.OData.Edm.IEdmEntityType;
IEdmStructuralProperty property = entityType.DeclaredStructuralProperties().Single(p => p.Name == "Dd");
IEdmType edmType=property.Type.Definition;
Upvotes: 2