ASP.NET 具有多个连接的 linq Select
ASP.NET linq Select with multiple joins
我首先从 EF 代码获得了这个数据库,现在我很难获得我的数据。
我有以下实体:
程序 : 是包含锻炼的锻炼程序。
锻炼:是包含一系列组的日常例程。
Set : 是一个练习,重复 x 次,负载为 y。
Exercise: 是包含Region的健身房运动。
区域 : 是人体上的一个区域,包含肌肉。
肌肉 : 是人体上的一块肌肉。
模型示例
这里有3个模型示例
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; }
}
和脚手架控制器
// GET: api/Workouts
public IQueryable<Workout> GetWorkouts()
{
return db.Workouts;
}
所以这只给我一个 Workout 对象,其 iCollection Sets 为空。
// GET: api/Workouts
public IQueryable<Workout> GetWorkouts()
{
return db.Workouts.Include(w => w.Sets);
}
通过包含Sets我可以获得相应的Sets。
但是,如果我想要进行锻炼,包括集合,而这些集合将包括练习,而这些练习将包括最终有肌肉列表的区域怎么办?
如果我想通过 id 进行锻炼怎么办,如下所示:
[ResponseType(typeof(Workout))]
public IHttpActionResult GetWorkout(int id)
{
Workout workout = db.Workouts.Find(id);
if (workout == null)
{
return NotFound();
}
return Ok(workout);
}
然后我将如何包含 集 ?
编辑
从 Visual Studio 模板/帮助页面,我可以看到以下内容:
GET api/Workouts/{id}
应提供医学:
{
"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"
}
]
}
},
但这不会发生,我对任何 returns 我都开放 JSON 中的完整程序。 Skype、teamwievr、stack、另一种解决方案、anyhting。
如果模型设置正确,链接 Include()
方法应该有效。
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))));
}
终于我明白了:
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 -.-
}
我首先从 EF 代码获得了这个数据库,现在我很难获得我的数据。
我有以下实体:
程序 : 是包含锻炼的锻炼程序。
锻炼:是包含一系列组的日常例程。
Set : 是一个练习,重复 x 次,负载为 y。
Exercise: 是包含Region的健身房运动。
区域 : 是人体上的一个区域,包含肌肉。
肌肉 : 是人体上的一块肌肉。
模型示例
这里有3个模型示例
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; }
}
和脚手架控制器
// GET: api/Workouts
public IQueryable<Workout> GetWorkouts()
{
return db.Workouts;
}
所以这只给我一个 Workout 对象,其 iCollection Sets 为空。
// GET: api/Workouts
public IQueryable<Workout> GetWorkouts()
{
return db.Workouts.Include(w => w.Sets);
}
通过包含Sets我可以获得相应的Sets。
但是,如果我想要进行锻炼,包括集合,而这些集合将包括练习,而这些练习将包括最终有肌肉列表的区域怎么办?
如果我想通过 id 进行锻炼怎么办,如下所示:
[ResponseType(typeof(Workout))]
public IHttpActionResult GetWorkout(int id)
{
Workout workout = db.Workouts.Find(id);
if (workout == null)
{
return NotFound();
}
return Ok(workout);
}
然后我将如何包含 集 ?
编辑 从 Visual Studio 模板/帮助页面,我可以看到以下内容:
GET api/Workouts/{id}
应提供医学:
{
"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"
}
]
}
},
但这不会发生,我对任何 returns 我都开放 JSON 中的完整程序。 Skype、teamwievr、stack、另一种解决方案、anyhting。
如果模型设置正确,链接 Include()
方法应该有效。
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))));
}
终于我明白了:
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 -.-
}