We are using Microsoft ASP.NET MVC OData WebAPI for our web services. Because of some data architecture issues surrounding hierarchy ID (which are outside the scope of this
The problem is in this line of code,
var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast();
The Cast is invalid as once $select and $expand are applied the result is no longer a Person. It would be a Wrapper that contains only the properties that the client asked for. You might have to modify your HierarchyNodeExpressionVisitor to take this into consideration.
Also, try changing your action to this to handle the fact that the result might not be a IQueryable anymore.
public IHttpActionResult Get(ODataQueryOptions oDataQueryOptions)
{
IQueryable result;
IQueryable dataSet = context.Persons;
IQueryable tempQuery = oDataQueryOptions.ApplyTo(dataSet);
var modifier = new HierarchyNodeExpressionVisitor(GetDescendantsOfNode, GetAncestorsOfNode);
var expression = modifier.ModifyHierarchyNodeExpression(tempQuery.Expression);
result = context.Persons.Provider.CreateQuery(expression);
return Ok(result, result.GetType());
}
private IHttpActionResult Ok(object content, Type type)
{
Type resultType = typeof(OkNegotiatedContentResult<>).MakeGenericType(type);
return Activator.CreateInstance(resultType, content, this) as IHttpActionResult;
}