WebAPI 2 Odata筛选器不起作用 [英] WebAPI 2 Odata Filter not working

查看:89
本文介绍了WebAPI 2 Odata筛选器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WebAPI 2 Odata项目上使用多个过滤器时遇到问题.

I have a problem with using multiple filters on my WebAPI 2 Odata project.

我们只需要JSON输出,并且只查询一种对象类型,因此我们将url设置为"/",而无法使用其他控制器.

We only want JSON output and only have one object type to query so we set the url to "/" without the possibility to use a different controller.

我有一个要查询的对象,它具有以下属性:

I have an object I want to query with the following properties:

 public class Content
 {
    public int Id { get; set; }
    public string Title { get; set; }
    public string Excerpt { get; set; }
    public string Link { get; set; }
    public IList<Tag> Tags  { get; set; }
 }

 public class Tag 
 {
    public string Id { get; set; }
    public string Name { get; set; }
 }

控制器代码如下:

public class ContentController : ApiController
{

    private readonly IContentRepository _repository;

    // constructor
    public ContentController(IContentRepository repository)
    {
        _repository = repository;
    }

    [Queryable]
    public IQueryable<Content> Index()
    {
        // IContentRepository.GetAll returns an IEnumerable List of Content
        return _repository.GetAll().AsQueryable();
    }
}

现在,我模拟了一些测试数据,向存储库中添加了多个对象,这些对象具有多个标记,这些标记的值设置为(test1,test2或test3).现在,当我查询

Now, I've mocked some testdata with adding multiple objects to the repository that have multiple tags with values set to either (test1, test2 or test3). Now when i Query

http://localhost:xxx?$filter=Tags/any(o: o/Id eq 'test1')

我得到了所有将Tag/Id设置为'test1'的对象.但是,如果我查询

I get all objects with Tag/Id set to 'test1'. But if I query

http://localhost:xxx?$filter=Tags/any(o: o/Id eq 'test1' and o/Id eq 'test2')

我没有结果(JSON return = []).但是它应该返回同时具有两个标签的对象.

I get no result (JSON return = []). But it should return objects that have both tags.

我在做什么错了?

我的样本数据JSON看起来像这样:

My sample data JSON looks like this:

[
 {
   "Id": 1,
   "Title": "TESTOBJECT 1",
   "Excerpt": "",
   "Link": "",
   "Tags": [
             {
               "Id": "test1",
               "Name": "Test Tag 1",
             }
           ],
 },
 {
   "Id": 2,
   "Title": "TESTOBJECT 2",
   "Excerpt": "",
   "Link": "",
   "Tags": [
             {
               "Id": "test2",
               "Name": "Test Tag 2",
             }
           ],
 },
 {
   "Id": 3,
   "Title": "TESTOBJECT 3",
   "Excerpt": "",
   "Link": "",
   "Tags": [
             {
               "Id": "test3",
               "Name": "Test Tag 3",
             }
           ],
 },
 {
   "Id": 4,
   "Title": "TESTOBJECT 4",
   "Excerpt": "",
   "Link": "",
   "Tags": [
             {
               "Id": "test1",
               "Name": "Test Tag 1",
             },
             {
               "Id": "test2",
               "Name": "Test Tag 2",
             }
           ],
 },
 {
   "Id": 5,
   "Title": "TESTOBJECT 5",
   "Excerpt": "",
   "Link": "",
   "Tags": [
             {
               "Id": "test1",
               "Name": "Test Tag 1",
             }
           ],
 }
]

现在查询1给我对象1,4,5,而我希望查询2给我对象4.我该如何用odata做到这一点?

Now query one gives me object 1,4,5 and I would expect query two to give me object 4. How can I accomplish this with odata?

推荐答案

您想要这个吗?

http://localhost:xxx?$filter=Tags/any(o: o/Id eq 'test1') and Tags/any(o: o/Id eq 'test2')

这篇关于WebAPI 2 Odata筛选器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆