从没有错误"一个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 与的WorkoutSession列表与练习有关。 WorkoutSession有许多WorkoutSessionExercise,而这些人有只练习一个1对1的关系。
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有错误:类型参数不能从查询推断
The last FROM has the error : The type argument cannot be inferred from the query.
我怎样才能加载这个三级LINQ的深对象要实体?
How can I load this three level deep objects with Linq To Entity?
推荐答案
?请问像这样的工作。
Would something like this work?
DatabaseContext.SetOwnable<WorkoutSession>
.Include("WorkoutSessionExercises.Exercise")
.Where(w => w.Id == id);
替代语法:
Alternate syntax:
from workoutSession in DatabaseContext.SetOwnable<WorkoutSession>
.Include("WorkoutSessionExercises.Exercise")
where workoutSession.Id == id
select workoutSession;
这里的关键是,包含
法 - 这使您可以表明哪些相关的对象应当水分。
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 Linq Nested Entities):
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屋!