从DbSet 3表返回无错误“不能从查询中推断". [英] Returning from a DbSet 3 tables without the error "cannot be inferred from the query"
问题描述
我有3个班级.WorkoutSession,WorkoutSessionExercise和锻炼.
I have 3 classes. WorkoutSession, WorkoutSessionExercise and Exercise.
我想返回WorkoutSession with 与Workout相关的WorkoutSession with 列表.WorkoutSession 有很多 WorkoutSessionExercise,而这一个与锻炼只有一对一的关系.
I would like to return the WorkoutSession with its list of WorkoutSession with the Exercise related. WorkoutSession has many WorkoutSessionExercise, and these one has only a single 1 to 1 relationship with Exercise.
var query = from workoutSession in DatabaseContext.SetOwnable<WorkoutSession>()
from workoutSessionExercises in workoutSession.WorkoutSessionExercises
from exerciseInfo in workoutSessionExercises.Exercise
where workoutSession.Id == id
select workoutSession;
最后一个FROM出现错误:无法从查询中推断出type参数.
The last FROM has the error : The type argument cannot be inferred from the query.
如何使用Linq To Entity加载这三层深的对象?
How can I load this three level deep objects with Linq To Entity?
推荐答案
这样的作品行吗?
DatabaseContext.SetOwnable<WorkoutSession>
.Include("WorkoutSessionExercises.Exercise")
.Where(w => w.Id == id);
备用语法:
from workoutSession in DatabaseContext.SetOwnable<WorkoutSession>
.Include("WorkoutSessionExercises.Exercise")
where workoutSession.Id == id
select workoutSession;
这里的关键是 Include
方法-这使您可以指示哪些相关对象应该被水合.
The key here is that Include
method - this allows you to indicate which related objects should be hydrated.
修改
尝试解决基于字符串的包含问题(包含使用LINQ的嵌套实体的启发):
Try this to get around the string-based includes (inspiration from Include nested entities using LINQ):
var query = from workoutSession in DatabaseContext.SetOwnable<WorkoutSession>
select new
{
WorkoutSession,
WorkoutSessionExercises = from workoutSessionExercise in
DatabaseContext.SetOwnable<WorkoutSessionExercises>
select new
{
WorkoutExercise = from workoutExercise in
DatabaseContext.SetOwnable<WorkoutExercise>
select workoutExercise
}
};
var results = query.Select(r => r.WorkoutSession).Where(w => w.Id == id);
这篇关于从DbSet 3表返回无错误“不能从查询中推断".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!