Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to enable nested json result in OData (ASP.NET Core API)

How to enable nested json result in OData (ASP.NET Core API)

Scheduled Pinned Locked Moved ASP.NET
asp-netjsonhelpquestioncsharp
3 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Wright 2022
    wrote on last edited by
    #1

    I'm using the following controller to get data from database using EF Core:

    public class EquipmentsController : ODataController
    {
    private readonly SqlServerContext _sqlServerContext;

        public EquipmentsController(SqlServerContext sqlServerContext)
        {
            \_sqlServerContext = sqlServerContext;
        }
    
        \[HttpGet\]
        \[EnableQuery\]
        public ActionResult\> Get()
        {
            IQueryable eqList = \_sqlServerContext.Equipments.AsQueryable()
                .Include(x => x.CostCenter)
                .Include(x => x.EquipmentCategory)
                .Include(x => x.equipmentType);
    
            return Ok(eqList);
        }
    }
    

    My problem is that I cannot get nested result in the final json while I have included CostCenter, EquipmentCategory, and equipmentType entities. I'm using ASP.NET Core 6 API and OData 8.0.12 My ED model is as follows:

    static IEdmModel GetEdmModel()
    {
    ODataConventionModelBuilder builder = new();
    builder.EntitySet("Equipments");
    return builder.GetEdmModel();
    }

    How can I fix that?

    Richard DeemingR 1 Reply Last reply
    0
    • A Alex Wright 2022

      I'm using the following controller to get data from database using EF Core:

      public class EquipmentsController : ODataController
      {
      private readonly SqlServerContext _sqlServerContext;

          public EquipmentsController(SqlServerContext sqlServerContext)
          {
              \_sqlServerContext = sqlServerContext;
          }
      
          \[HttpGet\]
          \[EnableQuery\]
          public ActionResult\> Get()
          {
              IQueryable eqList = \_sqlServerContext.Equipments.AsQueryable()
                  .Include(x => x.CostCenter)
                  .Include(x => x.EquipmentCategory)
                  .Include(x => x.equipmentType);
      
              return Ok(eqList);
          }
      }
      

      My problem is that I cannot get nested result in the final json while I have included CostCenter, EquipmentCategory, and equipmentType entities. I'm using ASP.NET Core 6 API and OData 8.0.12 My ED model is as follows:

      static IEdmModel GetEdmModel()
      {
      ODataConventionModelBuilder builder = new();
      builder.EntitySet("Equipments");
      return builder.GetEdmModel();
      }

      How can I fix that?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Have you included the relevant $expand options in your query? OData Expand - OData | Microsoft Learn[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Have you included the relevant $expand options in your query? OData Expand - OData | Microsoft Learn[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        A Offline
        A Offline
        Alex Wright 2022
        wrote on last edited by
        #3

        Thanks. Solved.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups