我在我的WebApi控制器中使用ODATA。
Id ProductID SubProductID ProductName
1 111 NULL Car
2 111 666 Wheels
3 111 777 Seats
4 112 123 brakes https://localhost:30004/api/ProductManagement/Product?$filter=ProductName eq 'Car‘
汽车是主要的产品和车轮,座椅是subproducts.When我做以上调用它将检索‘汽车’,但我的要求是也获得与汽车相关的子产品。
汽车ProductID是111,车轮、座椅ProductID也是111。
所以我正在寻找一个ODATA查询,它将返回Car及其subproducts.Something,如下所示,但我只知道ProductName的名称"Car“,而不知道ProductID。
我的要求是在相同的调用或查询中使用ProductName "Car“并使用其ProductID "111”来获取记录。
https://localhost:30004/api/ProductManagement/Product?$filter=ProductName eq 'Car‘和ProductID = Product.ProductID
发布于 2018-02-15 02:01:31
为了实现所需的行为,更改了表结构,如下所示
Id ProductID ParentProductID ProductName
1 111 NULL Car
2 666 111 Wheels
3 777 111 Seats
4 123 112 brakes
5 112 null secondparent已创建productid上的主键和parentproductid上引用同一表上的productid的外键。
所以当我从DB添加实体模型时,它创建了下面的类
public partial class ProductMangement
{
public ProductMangement()
{
this.ProductMangement1 = new HashSet<ProductMangement>();
}
public int id { get; set; }
public int productid { get; set; }
public Nullable<int> subproductid { get; set; }
public string productname { get; set; }
public virtual ICollection<ProductMangement> ProductMangement1 { get; set; }
public virtual ProductMangement ProductMangement2 { get; set; }
} 并在OData选项中使用$expand、$filter,如下所示
http://localhost:2233/api/ProductMangements?$filter=productname%20eq%20%27car%27&$expand=ProductMangement1
低于JSON数据
[{"id":1,"productid":111,"subproductid":null,"productname":"car","ProductMangement1":{"id":2,"productid":666,"subproductid":111,“productname”:“车轮”},{"id":3,"productid":777,"subproductid":111,"productname":"Seats"}}]
https://stackoverflow.com/questions/48781117
复制相似问题