[英]Entity Framework with Include and Select together
I have the following entities ( pseudo code to save space)我有以下实体(节省空间的伪代码)
Program [ int Id,
string Name,
List<ProgramFoodType> ProgramFoodTypes,
List<ProgramFood> ProgramFoods]
ProgramFoodType[ int Id, int ProgramId, int Type, bool IsActive]
ProgramFood [ int Id, int ProgramId, Food Food, FoodType FoodType]
Food [int Id, string Name]
FoodType [int Id, string Name]
my task is to get single Program
with its related ProgramFoodTypes
with condition ProgramFoodType should be active and ProgramFoods
with related entities Food
and FoodType
我的任务是获取单个Program
及其相关ProgramFoodTypes
,条件 ProgramFoodType 应处于活动状态,并且ProgramFoods
与相关实体Food
和FoodType
I used the following so far到目前为止,我使用了以下内容
1- the below query will retrieve the details of ProgramFoodTypes
and ProgramFoods
but it will bring all active and inactive ProgramFoodTypes
1- 以下查询将检索ProgramFoodTypes
和ProgramFoods
的详细信息,但它将带来所有活动和非活动ProgramFoodTypes
var program = mEntities.Programs
.Include(p =>p.ProgramFoodTypes)
.Include(p =>p.ProgramFoods.Select(f =>f.Food))
.InClude(p =>p.ProgramFoods.Select( f =>f.FoodType))
.Where(m =>m.Id== Id);
2- the below query will retrieve the details but missing the Food
and FoodType
2- 以下查询将检索详细信息但缺少Food
和FoodType
var program = (from p in mEntities.Programs
where p.Id ==Id
select new {
Program = p,
ProgramFoodTypes = from pf in p.ProgramFoodTypes
where pf.IsActive
select pf,
ProgramFoods = p.ProgramFoods // here i can't add include statement
}).ToArray().Select(m => m.Program);
how to include the food and food type in the second query?如何在第二个查询中包含食物和食物类型?
For your second solution, I think you can use:对于您的第二个解决方案,我认为您可以使用:
var program = (from p in mEntities.Programs
.Include(p => p.ProgramFoods.Select(f => f.Food))
.Include(p => p.ProgramFoods.Select(f => f.FoodType))
where p.Id == Id
select new {
Program = p,
ProgramFoodTypes = from pf in p.ProgramFoodTypes
where pf.IsActive
select pf,
p.ProgramFoods
}).ToArray().Select(m => m.Program);
update : Since you're using anonymous type in your linq query, Include statements are dismissed.更新:由于您在 linq 查询中使用匿名类型, Include 语句被忽略。
You'd have to load all related ProgramFoodTypes
on client side, and then do the filtering:您必须在客户端加载所有相关的ProgramFoodTypes
,然后进行过滤:
var program = mEntities.Programs
.Include(p => p.ProgramFoodTypes)
.Include(p => p.ProgramFoods.Select(f => f.Food))
.Include(p => p.ProgramFoods.Select(f => f.FoodType))
.SingleOrDefault(m => m.Id == Id);
program.ProgramFoodTypes = program.ProgramFoodTypes.Where(pft => pft.IsActive);
You can use AsNoTracking()
or clone the returned Program
object in a new object in case you want to make sure your data will be intact on db-side.您可以使用AsNoTracking()
或在新对象中克隆返回的Program
对象,以防您想确保数据在 db 端完好无损。
Try this:尝试这个:
var program = mEntities.Programs
.Include(p => p.ProgramFoodTypes)
.Include(p => p.ProgramFoods.Select(f => f.Food))
.InClude(p => p.ProgramFoods.Select(f => f.FoodType))
.SingleOrDefault(m => m.Id == Id && m.ProgramFoodTypes.All(t => t.IsActive));
may be:或许:
var program = (from p in mEntities.Programs
where p.Id ==Id
select new {
Program = p,
ProgramFoodTypes = from pf in p.ProgramFoodTypes
where pf.IsActive
select pf,
ProgramFoods = p.ProgramFoods.Select(y => new {
Food = y.Food,
Type = y.FoodType
})
}).ToArray().Select(m => m.Program);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.