问题
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