Filter Child Property OData and ASPNET WebApi

孤者浪人 提交于 2019-12-12 16:58:39

问题


I'm facing a problem. I've googled a lot and searched in here too, but I couldn't find a answer to this problem.

I have a ASPNET Web Api that returns JSON. I also, add Microsoft.Data.OData nuget to allow odata queries.

It's working fine for simple cases, but now, I need to apply a filter in a child collection.

Sample:

{"total":1,"products":[{"id":20289,"brandId":5,"categoryId":1,"price":12.0,"name":"Carolina Herrera","description":"CH","productCode":"asd2334","picture":null,"contentPackaging":"liquid","brandName":"Carolina Herrera","brandPicture":null,"generic":true},
{"id":20290,"brandId":5,"categoryId":1,"price":25.0,"name":"Carolina Herrera 2","description":"CH 2","productCode":"asd999","picture":null,"contentPackaging":"liquid","brandName":"Carolina Herrera","brandPicture":null,"generic":true}
]}

I want to query in products collection where price is greater than 20 for example.

I've tried something like this:

http://domain.com/api/$filter=products/price gt 20

But that didn't work.

Is it possible to do that?

Code:

//controller
public IQueryable<ViewModelProducts> GetProducts()
{
    var products = _repository.FindBy(x=>x.Generic && x.Status).ToList();

    return products.Count == 0 ? new List<ViewModelProducts>().AsQueryable() : products.AsQueryable();
}

//model
public class ViewModelProducts
{
    public int total { get; set; }
    public IQueryable<Products> products { get; set; }
}

public class Products
{
    public int id { get; set; }
    public int brandId { get; set; }
    public int categoryId { get; set; }
    public decimal price { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string productCode { get; set; }
    public string picture { get; set; }
    public string contentPackaging { get; set; }
    public string brandName { get; set; }
    public string brandPicture { get; set; }
    public bool generic { get; set; }
}

来源:https://stackoverflow.com/questions/17971798/filter-child-property-odata-and-aspnet-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!