我面临一个问题。我用谷歌搜索了很多,也在这里搜索过,但我找不到这个问题的答案。
我有一个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; }
}