首先,使用ASP.NET WebApi教程,我创建了一个基本的
ApiController that exposes an Entity Framework model through OData.该服务用于返回json for OData $filter查询.
当我对多值属性执行OData $filter queries that include “any” or “all”查询时会抛出一个ODataException
这是我试图使用的OData查询
/ api / Blogs?$filter = any(Tags,Name eq’csharp’)
我的ApiController看起来像这样:
public class BlogController : ApiController
{
public BlogsController()
{
this.Entities = new BlogEntities();
}
public ContactEntities Entities { get; set; }
[Queryable(PageSize = 25,AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<Blog> Get()
{
return this.Entities.Blogs;
}
}
博客实体有这个合同
public Blog {
public Guid ID { get; set; }
public string Title { get; set; }
public Tag Tags { get; set; }
}
public Tag {
public Guid ID { get; set; }
public string Name { get; set; }
}
抛出异常
ODataException: Type 'Blog' does not have a property 'Name'
正如你所看到的,我的代码中没有什么是普通的,一切都应该正常工作.可能在Microsoft ASP.NET Web API OData中不支持“任何”和“全部”查询?
解决方法
你的任何需要改变一点.尝试这样的东西:
~/api/Blogs?$filter=Tags/any(tag: tag/Name eq 'csharp')
这是假设标签实际上返回一个标签的集合,而不仅仅是一个单一的标签,就像你以上.
$inlinecount仅支持OData格式的开箱即用.我在这里写道:
Web API OData Inlinecount not working
简单的答案是,您可以使其他格式的代码看起来像这样:
public PageResult<Customer> Get(ODataQueryOptions<Customer> queryOptions)
{
IQueryable results = queryOptions.ApplyTo(_customers.AsQueryable());
return new PageResult<Customer>(results as IEnumerable<Customer>,Request.GetNextPageLink(),Request.GetInlineCount());
}
