You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core OData 8.2.3如何筛选关联实体Customer指定字段?

解决OData关联实体字段筛选问题

针对你的场景,在Microsoft.AspNetCore.OData 8.2.3中,要同时限制主实体Order和关联实体Customer的返回字段,正确的URL写法是在$expand中嵌套$select来指定关联实体的字段,示例如下:

https://localhost:7065/api/Order?$expand=Customer($select=Name)&$select=Id,Orderno

关键注意点

  • 确认OData模型构建时,已正确配置Order与Customer的导航属性,比如在实体配置中通过HasOne(o => o.Customer)显式声明关联关系。
  • 若你使用的是普通Web API路由(如[Route("api/[controller]")]),部分OData查询语法支持受限,建议改用OData专用路由模板:
    [ODataRoutePrefix("Order")]
    public class OrdersController : ODataController
    {
        [ODataRoute]
        public IQueryable<Order> Get()
        {
            // 业务查询逻辑
        }
    }
    
    此时访问路径需调整为:
    https://localhost:7065/Order?$expand=Customer($select=Name)&$select=Id,Orderno
    

内容的提问来源于stack exchange,提问作者Jürgen Steinblock

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 18:59:55