我们将Microsoft ASP.NET MVC OData WebAPI用于我们的Web服务.由于围绕层次结构ID的某些数据架构问题(这些问题超出了此对话的范围),我们的一些GET操作必须使用ODataQueryOptions并手动操作表达式以添加其他限制.我们这样做(删除错误处理代码并调用内联的其他方法)
public IQueryable<Person> Get(ODataQueryOptions<Person> oDataQueryOptions)
{
IQueryable<Person> result;
IQueryable<Person> dataSet = context.Persons;
var tempQuery = oDataQueryOptions.ApplyTo(dataSet).Cast<Person>();
var modifier = new HierarchyNodeExpressionVisitor(GetDescendantsOfNode, GetAncestorsOfNode);
var expression = modifier.ModifyHierarchyNodeExpression(tempQuery.Expression);
result = context.Persons.Provider.CreateQuery<Person>(expression);
return result;
}
Run Code Online (Sandbox Code Playgroud)
这已经有一段时间了,但是我们一直急切地等待选择和扩展,以便我们能够更好地控制从服务中获得的数据.星期一我们将开发环境更新为WebApi OData 5.0.0-rc1并进行了选择和扩展工作,但是我们不能将它用于使用ODataQueryOptions的这些服务.我们只能用它来对付我们的其他服务.如果我们使用$select和/或查询上面的代码$expand,我们会收到以下错误:
"message": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
"type": "System.InvalidOperationException",
"stacktrace": "",
"internalexception":
{
"message": "Unable to cast the type 'System.Web.Http.OData.Query.Expressions.SelectAllAndExpand`1' to type 'OurCompany.Domains.Data.Models.Person'. LINQ to Entities only supports casting …Run Code Online (Sandbox Code Playgroud)