我有一个问题:(我在两个表(1和2)之间有一个多对多的表,通过映射表(3):
(1)Trees / (2)Insects
TreeID <- (3)TreeInsects -> InsectID 然后是一对多关系:
Trees.ID -> Leaves.TreeID我想执行一个查询,它将给我一个昆虫集合的所有叶子(通过树-昆虫映射表)。
例如,我有List<Insects>,我想要通过树-昆虫映射表与列表中的任何昆虫有关联的所有叶子。
这似乎是一项简单的任务,但由于某些原因,我在做这件事上遇到了麻烦!
我所拥有的最好的:但是Single()使它不正确:
from l in Leaves
where (from i in Insects
select i.ID)
.Contains((from ti in l.Tree.TreeInsects
select ti.InsectID).Single())
select l;发布于 2011-06-10 04:13:48
我不擅长使用类似sql的语法,所以我将使用扩展来编写。
ctx.Leaves.Where(l => ctx.TreeInsects.Where( ti => list_with_insects.Select(lwi => lwi.InsectID).Contains( ti.InsectID ) ).Any( ti => ti.TreeID == l.TreeID ) );发布于 2011-06-10 04:20:30
(from i in insectsCollection
select from l in Leaves
let treeInsectIDs = l.Tree.TreeInsects.Select(ti => ti.InsectID)
where treeInsectIDs.Contains(i.ID)
select l)
.SelectMany(l => l)
.Distinct();发布于 2011-06-10 04:13:22
尝试研究SelectMany方法-我认为这可能是您需要的关键。
我将获得该昆虫可用的树的列表,然后将SelectMany挂在最后,并取出与该树相关的树叶集合。
https://stackoverflow.com/questions/6298469
复制相似问题