我想按
subCategories
过滤产品。
这是我的产品 JSON:
{
"id": 1,
"name": "Adidas Quick Force",
"price": 3500.00,
"pictureUrl": "/adidas_shoe-1.png",
"category": {
"id": 1,
"name": "FootBall",
"subCategories": [
{
"id": 1,
"name": "FootBall Boots"
},
{
"id": 2,
"name": "FootBalls"
}
]
}
}
我可以通过以下方法按
Category
进行过滤:
public static IQueryable<Product> FilterCategory(this IQueryable<Product> products, string name)
{
if (name != null)
{
return products.Where(e => (e.Category.Name == name) );
}
return products;
}
我尝试了这个,但它给出了一个错误:
public static IQueryable<Product> FilterCategory(this IQueryable<Product> products, string name)
{
if (name != null)
{
return products.Where(e => (e.Category.subCategories.Name == name) ); // error is here
}
return products;
}
注意
SubCategories
是一个数组/列表,您不能直接访问 Name
属性。
.Any()
,以过滤包含 subCategories
数组中名称匹配的嵌套对象的产品。
return products.Where(e => e.Category.subCategories.Any(x => x.Name == name));