[英]Get parent object based on child of child criteria and include
Ok so here is a quick version of my tables. 好的,这是我的桌子的快速版本。
public class Line
{
// Bunch Of properties
// ...
public List<Tool> Tools {get;set;}
}
public class Tool
{
// Bunch Of properties
// ...
public List<Task> Tasks {get;set;}
}
public class Task
{
// Bunch Of properties
// ...
public List<SchedItem> SchedItems {get;set;}
}
public class SchedItem
{
public DateTime SchedDate {get;set;}
}
What I want is a query that will return all the Lines
including child collections and their child collections etc based on whether SchedItem.SchedDate
falls within a date range. 我想要的是一个查询,该查询将根据
SchedItem.SchedDate
是否在日期范围内返回包括SchedItem.SchedDate
等在内的所有Lines
。
I tried the following in LinqPad. 我在LinqPad中尝试了以下方法。
var line = (from p in Lines.Include(x => x.Tools.Select(m => m.Tasks.Select(s => s.SchedItems)))
from t in p.Tools
from m in t.Tasks
from s in m.SchedItems
where s.SchedDate >= StartDateQuery &&
s.SchedDate <= EndDateQuery
select p);
This seems wrong to me as the returned data is including all SchedItems
not just the ones based on the query. 这对我来说似乎是错误的,因为返回的数据包括所有
SchedItems
而不仅仅是基于查询的那些。
UPDATE 1: Yeah I think I have my understanding wrong. 更新1:是的,我想我的理解是错误的。 I have set the following on my context configuration
我在上下文配置中设置了以下内容
db.Configuration.LazyLoadingEnabled = false;
db.Configuration.ProxyCreationEnabled = false;
When I run the code again I get a list of 33 Line
items with no child items. 当我再次运行代码时,我得到了33个没有子项的
Line
项目的列表。 There should only be 1 'Line' item with many child items. 只能有多个子项的1个“行”项。
I changed my query to this and I now get the child items but the Line
count is still 33 which is wrong. 我将查询更改为此,现在我得到了子项,但“
Line
数”仍为33,这是错误的。
var line = (from p in Lines
from t in p.Tools
from m in t.Tasks
from s in m.SchedItems
where s.SchedDate >= StartDateQuery &&
s.SchedDate <= EndDateQuery
select p).Include(x => x.Tools.Select(m => m.Tasks.Select(s => s.SchedItems)));
Try this 尝试这个
var lines = Lines.Where(i =>
i.Tools.Any(t =>
t.Tasks.Any(y =>
y.SchedItems.Any(u =>
u.SchedDate >= StartDateQuery &&
s.SchedDate <= EndDateQuery
)
)
)
);
lines.ForEach(i => new
{
i.Tools = i.Tools.Where(t =>
t.Tasks.Any(y =>
y.SchedItems.Any(u =>
u.SchedDate >= StartDateQuery &&
s.SchedDate <= EndDateQuery
)
)
);
i.Tools.ForEach(t => new
{
t.Tasks = t.Tasks.Where(y =>
y.SchedItems.Any(u =>
u.SchedDate >= StartDateQuery &&
s.SchedDate <= EndDateQuery
)
);
});
});
Your code make cartesian product because of couple from
statements. 您的代码由于使用了一对
from
语句而生成笛卡尔积。 I'll skip configuration of DbContext and assume that you have enabled lazy loading. 我将跳过DbContext的配置,并假定您已启用延迟加载。 Try this:
尝试这个:
var line = from p in ls
where p.Tools
.Any(to=> to.Tasks
.Any(ta=> ta.SchedItems
.Any(s=> s.SchedDate >= StartDateQuery && s.SchedDate <= EndDateQuery)));
select p;
Another way to accomplish your goal is to add parrent to: SchedItem, Task, Tool, find SchedItems that meet your requirements and navigate through parents to Line. 实现目标的另一种方法是添加权限:SchedItem,Task,Tool,找到满足您要求的SchedItem,并通过父级导航到Line。 For more information go here
欲了解更多信息,请点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.