【问题标题】:How can we get Odata $expand result with subquery in expanded result我们如何在扩展结果中使用子查询获得 Odata $expand 结果
【发布时间】:2020-06-11 02:53:30
【问题描述】:

有以下型号

  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/Product?$expand=Category&$select=Price

如何使用 odata 实现这一目标?

【问题讨论】:

    标签: asp.net-core entity-framework-core odata


    【解决方案1】:

    如果我理解正确,您可以使用 $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"。

    谢谢,希望对你有帮助。

    【讨论】:

    • 感谢您的回复。但在我的情况下,子查询更复杂,并且在引用表中没有任何键。我已经在我的帖子中添加了这些细节。请提供您的建议,如何使用 Odata 获得它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2012-06-03
    • 2017-02-09
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多