Dot-Net
在where子句中使用數組的Linq查詢?
我已經搜尋過這個,但似乎仍然無法讓它為我工作。我有一個與使用者關聯的 ID 數組(他們的組織 ID)。它們被放置在 int[] 中,如下所示:
int[] OrgIds = (from oh in this.Database.OrganizationsHierarchies join o in this.Database.Organizations on oh.OrganizationsId equals o.Id where (oh.Hierarchy.Contains(@OrgId)) || (oh.OrganizationsId == Id) select o.Id).ToArray();那裡的程式碼不是很重要,但它表明我從 Linq 查詢中獲取了一個整數數組。
不過,我想執行另一個獲取人員列表的 Linq 查詢,程式碼如下:
List<Personnel> query = (from p in this.Database.Personnels where (search the array) select p).ToList();我想在 where 子句中添加一種僅選擇數組中具有 OrganizationId 的使用者的方法。因此,在 SQL 中,我會執行類似“其中 OrganizationId = ‘12’ 或 OrganizationId = ‘13’ 或 OrganizatonId = ‘17’”之類的操作。
我可以在 Linq / .NET 中相當容易地做到這一點嗎?
雖然這可能更適合加入,但您可以使用它:
List<Personnel> query = (from p in this.Database.Personnels where OrgIds.Contains(p.OrgID) select p).ToList();這將轉化為 SQL 之類的東西。
where OrgID in (1,2,...,n)