Asp.net core 如何在展开结果中使用子查询获得Odata$expand结果
仿效Asp.net core 如何在展开结果中使用子查询获得Odata$expand结果,asp.net-core,entity-framework-core,odata,Asp.net Core,Entity Framework Core,Odata,仿效 public class Product { public string Barcode { get; set; } public double Price { get; set; } public Category Category { get; set; } } public class Category { public string Name { get; set; } public string department
public class Product {
public string Barcode { get; set; }
public double Price { get; set; }
public Category Category { get; set; }
}
public class Category {
public string Name { get; set; }
public string department { get; set; }
}
现在的问题是我不能在$expand查询选项中使用子查询。这里我给出了一个简单的例子,但实际上我必须执行一些复杂的查询作为子查询
因为我得到了这个:
"value": [
{
"Price": 500,
"Category": {
"Name": "Cricket bat"
"Department": 235
}
}
]
预期结果:
"value": [
{
"Price": 500,
"Category": {
"Name": "Cricket bat"
"Department": "Sport" //This result I want to get using subquery.
}
}
子查询的表结构-
Department Table
--------------------------------------------
DepCode |Id |Seq |ValueData
235 |1 |0 |1
235 |1 |1 |2
235 |2 |0 |Sport
235 |2 |1 |Food
--------------------------------------------
SubQuery:
select d2.ValueData
from department d1
left join department d2 on d2.id=2
and d1.DepCode = d2.DepCode
and d1.Seq = d2.Seq
where d1.DepCode=235
and d1.ValueId=1
and d1.ValueData=1
在DB中,我得到值1(d1.ValueData=1),使用上面的查询,我需要获取“Sport”
Odata查询:/Odata/产品?$expand=类别&$select=价格
如何使用odata实现这一点?如果我理解正确,您可以使用$expand中的嵌套查询选项进行子查询
/odata/Product?$expand=Category($select=Department)和$select=Price
你可以得到:
"value": [
{
"Price": 500,
"Category": {
"Department": "Sport"
}
}
请注意:
1) $expand可以嵌套“$filter、$orderby、$top、$skip、$select、$expand…”
2) 在最新版本中,$select还可以嵌套“$filter、$orderby、$top、$skip、$select”
谢谢,希望能对您有所帮助。谢谢您的回复。但在我的例子中,子查询更复杂,并且在引用表中没有任何键。我已经在我的帖子中添加了这些细节。请提供您的建议,我如何才能得到它与奥达塔。