0

I am working a bit backwards here and trying to create a web api from a provided JSON example that is being used for the front end. Here is the JSON format.

 {"Sections": [{
     "sectionID":"1",
     "sectionOrder":1,
     "sectionName":"Approach",
     "sectionText": "",
     "sectionContent": [{
         "contentID": "1",
         "contentTitle": "Definition",
         "contentText": "Lorem Ipsum",
         "contentImage": ""
     },
     {
         "contentID": "2",
         "contentTitle": "Vision",
         "contentText": "Lorem Ipsum",
         "contentImage": "image2.jpg"
     }]
}]}

I have created 2 tables (Section & Section Content linked by SectionContentID) and added the tables as models using entity framework. I have then created a controller to return the section table but now is where I am stuck on how to join the section content data into 1 JSON response.

My controller just looks like this:

public IQueryable<Sections> GetSections()
{
    //how do I add db.SectionsContent to Sections linked by the sectionContentID field?
    return db.Sections;
}
2
  • We need to know more about your objects structure. Also, what is db? Is it dbContext of EF? Commented Aug 8, 2018 at 20:59
  • 1
    Well if you create a entity-framework navigation property named SectionContent that would be an enumeration of SectionContent so normally the serializer can just serialize the response in one go and get the json you're looking for. Commented Aug 8, 2018 at 20:59

2 Answers 2

1

If you are using entity framework (EF) and your tables relationship type is one to many, you can use Include in lambda expression to return first table with data of second table.

return context.Entity1st.Include(x => x.Entity2nd);

or you can also do as bellow:

var entities = context.Entity1st;

foreach (var entity in entities)
{
    entity.Entity2nd = context.Entity2nd.Where(x => x.Entity1stId == entity.Id);
}

return entities;
Sign up to request clarification or add additional context in comments.

5 Comments

In your second example, entity would be a collection of entities, so the second line wouldn't work.
Where clause may has null, one or more than one result and if you define second entity in first entity as collection like public virtual ICollection<Entity2nd> Entity2nd { get; set; }, there would be no problem.
You misunderstand. class context : DbContext { public ICollection<Entity1st> Entity1st; } means that var entity = context.Entity1st is a collection.
Thanks @ErikPhilips. I've edited it according to your comment. 🌼
That's also very hard on the DB. You are executing Entity1st.Count + 1 statements. If you simply did a join it would be a single query.
1

I would start from defining DTO object, since returning database objects directly isn't the best practice, you usually don't want to revial all fields also it gives you more flexibility with changing DB structure without breaking API contract. Also you need your collection to be wrapped in Sections property, so you can't just return a list of objects. So, you need structure like:

public class SectionsResponse
{
    public List<SectionDTO> Sections {get;set;}
}

public class SectionDTO
{
    public string SectionID {get;set;}
    .... add other properties
    public List<ContentDTO> sectionContent {get;set;}
}

public class ContentDTO
{
   ... define your properties
}

Next step will be to implement mapping between your database object and DTO. You may use existing library for this purpose, e.g. AutoMapper

As for working with database you can apply eagerly loading from Entity Framework. In short it will look like:

return db.Sections.Include(x => x.Content);

or wrapped with DTO and AutoMapper:

return new SectionsResponse() { Sections = mapper.Map<List<Section>, List<SectionDTO>>(db.Sections.Include(x => x.Content)) };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.