user1828605
user1828605

Reputation: 1735

How to display data from many-to-many relationship using odata $expand?

I have a few database tables where some of them have many-to-many relationships. I'm using webapi and odata to fetch the data from the database. For instance:

TableA ---> TableB (one to one)
TableA ---> TableC (one to one)
TableA ---> TableD (one to one)
TableE --> TableD ---> Table F (many-to-many)

Now, since I'm using odata controller, I can use /odata/Claims?$expand=TableA,TableB,TableC,TableD which displays the data as it is expected. However, I can't display data from TableE and TableF using Id's from TableD. I'm not sure what code to show you, but here's the GET method

   [EnableQuery(AllowedQueryOptions =  System.Web.OData.Query.AllowedQueryOptions.All)]
    public IQueryable<TableA> Get()
    {
        return dbContext.DeniedClaims.AsQueryable();               
    }

Is there any way I can show the data from TableE and TableF in the same query using odata?

This is how the data shows up when I do the $expand

{
  "@odata.context":"http://localhost:58891/odata/$metadata#TableA","value":[
    {
      // some values,
      "Id": 1, // blah blah
      "TableB":{
          "Id":1,"FirstName":"Test","LastName":"Test",
      },"TableC":{
        "Id":1,
      },"TableD":{
        "Id":1,"TableERefId":1,"TableFRefId":1
      }
    }
}

Note TableD, instead of only displaying me TableERefId and TableFRefId, I want to see the values in TableE and TableF.

How can I do this?

Upvotes: 1

Views: 4522

Answers (1)

lencharest
lencharest

Reputation: 2925

If I understand your data model correctly, you want to do inline expansion more than 1 level deep. Assuming that the TableD entity type has navigation properties named TableE and TableF, append a nested $expand option to TableD as follows:

$expand=TableB,TableC,TableD($expand=TableE,TableF)

Upvotes: 2

Related Questions