ASP.NET linq Select with multiple joins

At first I use this database from EF code, and now it is difficult for me to get my data.

I have the following entities :

Program : A workout program containing workouts.

Training : This is a daily program containing a list of sets.

Set : This exercise is repeated x times with a load of y.

Exercise : An exercise in the gym that contains the region.

Region : This is an area on the human body that contains muscle.

Muscles : This is the muscle on the human body.

Model Examples

Here are 3 sample models

public class Workout { [Key] public int WorkoutId { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Set> Sets { get; set; } } public class Set { [Key] public int SetId { get; set; } //Foreign key for Exercise public int ExerciseId { get; set; } [ForeignKey("ExerciseId")] public Exercise Exercise { get; set; } public decimal Load { get; set; } public decimal Order { get; set; } } public class Exercise { [Key] public int ExerciseId { get; set; } public string Name { get; set; } public string Description { get; set; } //Foreign key for Region public int RegionId { get; set; } public Region Region { get; set; } } 

And the forest controller

 // GET: api/Workouts public IQueryable<Workout> GetWorkouts() { return db.Workouts; } 

That way only gives me a Workout object with its iCollection NULL sets.

 // GET: api/Workouts public IQueryable<Workout> GetWorkouts() { return db.Workouts.Include(w => w.Sets); } 

By including Sets, I can get the corresponding Sets.

But what if I would like to get a workout, including sets, and these sets will include exercises, and these exercises will include regions that, after all, had a list of muscles?

And what if I need training from id, like this:

  [ResponseType(typeof(Workout))] public IHttpActionResult GetWorkout(int id) { Workout workout = db.Workouts.Find(id); if (workout == null) { return NotFound(); } return Ok(workout); } 

How can I enable Sets

EDIT AND on the Visual Studio template / help page, I see the following:

 GET api/Workouts/{id} 

Must provide copper:

 { "workoutId": 1, "name": "sample string 2", "description": "sample string 3", "sets": [ { "setId": 1, "exercise": { "exerciseId": 1, "name": "sample string 2", "description": "sample string 3", "regionId": 4, "region": { "regionId": 1, "regionName": "sample string 2", "muscles": [ { "muscleId": 1, "latinName": "sample string 2", "dkName": "sample string 3", "enName": "sample string 4", "description": "sample string 5" }, { "muscleId": 1, "latinName": "sample string 2", "dkName": "sample string 3", "enName": "sample string 4", "description": "sample string 5" } ] } }, 

But that just doesn’t happen, I will open for everything that returns me the full program in JSON. skype, teamwievr, stack, another solution, anyhting.

+5
source share
2 answers

And finally, I got it:

  public class Exercise { [Key] public int ExerciseId { get; set; } public string Name { get; set; } public string Description { get; set; } //Foreign key for Region //public int RegionId { get; set; } public virtual Region Region { get; set; } //had to be virtual -.- } 
+3
source

If you have the correct models configured, use the Include() method chain.

 public IQueryable<Workout> GetWorkouts() { return db.Workouts.Include(w => w.Sets.AsQueryable().Include(s => s.Excercises.AsQueryable().Include(e => e.Regions.AsQueryable().Include(r => r.Muscles)))); } 
+2
source

Source: https://habr.com/ru/post/1236196/


All Articles