Filter OData and ASPNET WebApi child objects

I have a problem. I searched a lot and searched here, but I could not find an answer to this problem.

I have an ASPNET Web Api that returns JSON . I will also add Microsoft.Data.OData nuget to allow odata requests.

It works fine for simple cases, but now I need to apply a filter in the child collection.

Example:

 {"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 request in a product collection where the price is more than 20, for example.

I tried something like this:

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

But that did not work.

Can this be done?

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; } } 
+2
source share

Source: https://habr.com/ru/post/1235401/


All Articles