我遇到了问题。我在谷歌搜索了很多并在这里搜索过,但我找不到这个问题的答案。
我有ASPNET Web Api
,返回JSON
。我还添加Microsoft.Data.OData nuget以允许odata查询。
它适用于简单的情况,但现在,我需要在子集合中应用过滤器。
样品:
{"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}
]}
我想查询价格大于20的产品系列。
我尝试过这样的事情:
http://domain.com/api/$filter=products/price gt 20
但那没用。
有可能吗?
代码:
//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; }
}