[英]How to Query 'In clause' upon collection of Collection in Linq
我有一個Ids列表,我想對嵌套collection發起一個'In Clause'查詢。我該怎么做。 更詳細地說,我有2張桌子
表:A表:B
這兩個表都有各自的主鍵,它們正在使用另一個“表C”來存儲其映射詳細信息。 表C:表C包含兩列,分別是表A和表B的主鍵。現在,我從表A中獲得ID,並使用表C從B中獲得與A相關聯的所有記錄。 “查詢中”就像Linq中的Sql一樣,但是不能在表C的集合上觸發。
我知道在linq中我們也有WhereIn子句。 但它沒有顯示在列表中。
我的查詢就是這樣。
Context.B.Where(item=>item.C.WhereIn(item1=>Item1.AID==aid));
我想要類似該查詢的內容,但使用“ C”表示未顯示where子句以及我該怎么做。
試試這個:
// 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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.