如何查询'在条款'收集Linq的收藏品

时间:2014-05-23 12:05:31

标签: c# linq entity

我有一个ID列表,我想在嵌套集合上触发'In Clause'查询。我会怎么做。 更详细地说,我有2个表

表:A 表:B

这两个表都有各自的主键,它们使用另一个“表C”来存储它们的映射细节。 表:C 表C包含两列,它们都是表A和表B的主键。 现在我有来自表A的ID,并从B中获取与A相关联的所有记录。我希望像Linq中的Sql一样触发'In Query'但不能触发表C的集合。

我知道在linq中我们也有WhereIn子句。但它没有显示在它的列表中。

我的查询是这样的。

       Context.B.Where(item=>item.C.WhereIn(item1=>Item1.AID==aid));

我想要那样的查询,但是'C'没有显示where子句以及我应该做什么建议。

1 个答案:

答案 0 :(得分:1)

试试这个:

// Select the ids of the table B that are associated with ids in aIds.
var bIds = Context.C.Where(x=>aIds.Contains(x.aId)).Select(x=>x.bId);

// Get the record of table B, which their ids are in bIds
var result = Context.B.Where(x=>bIds.Contains(x.Id));