我已经搜索过这个,但似乎仍然无法让它为我工作。我有一组与用户相关联的 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 中,我会执行诸如“where 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)
关于.net - 在 where 子句中使用 Array 进行 Linq 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/836491/