gpt4 book ai didi

c# - LINQ to SQL 其中集合包含集合

转载 作者:太空狗 更新时间:2023-10-29 21:51:51 24 4
gpt4 key购买 nike

我有一个问题 :( 我在两个表 (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;

最佳答案

(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();

关于c# - LINQ to SQL 其中集合包含集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6298469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com