WebApi OData:$ filter'any'或'all'查询不起作用

Chr*_*ann 12 entity-framework odata asp.net-web-api

首先,使用ASP.NET WebApi教程,我创建了一个基本的ApiController,它通过OData公开了一个Entity Framework模型.该服务用于返回ODA $过滤查询的json.

当我在多值属性上执行包含"任何"或"所有"查询的 OData $ filter查询时,它会抛出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;
    }
}
Run Code Online (Sandbox Code Playgroud)

博客实体有此合同

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; }
}
Run Code Online (Sandbox Code Playgroud)

抛出异常

ODataException: Type 'Blog' does not have a property 'Name'
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我的代码中没有任何异常,一切都应该正常工作.是否有可能在Microsoft ASP.NET Web API OData中不支持"任何"和"所有"查询?

You*_*oui 18

你的任何需要改变一下.尝试这样的事情:

~/api/Blogs?$filter=Tags/any(tag: tag/Name eq 'csharp')
Run Code Online (Sandbox Code Playgroud)

这假设Tags实际上返回了一个Tags集合,而不仅仅是像上面一样的单个Tag.

$ inlinecount仅支持OData格式的开箱即用.我在这里写了很多关于它的文章:

Web API OData Inlinecount无效

简短的回答是,您可以使用其他格式使用以下代码:

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());
}
Run Code Online (Sandbox Code Playgroud)