.net - 在 where 子句中使用 Array 进行 Linq 查询?

标签 .net sql 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 中,我会执行诸如“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/

相关文章:

c# - 无法将类型为 'System.Linq.Expressions.UnaryExpression' 的对象转换为类型 'System.Linq.Expressions.MemberExpression'

c# - 使用 linq 对 List<string> 的数组进行排序

c# - 可以在没有 Linq 的情况下使用 lambda 吗?

SQL:跨两个层次结构的完全外连接

c# - 使用 SemanticResultKey 时出现 TargetInvocationException

java - H2 在 Junit 测试中未按问题检测组

sql - 在 SQL Server 2005 中使用 SSIS 从平面文件导入时如何保留 NULL 值

c# - 不区分大小写的字符串比较在 C# 中不起作用?

c# - 将 MailItem 转换为内存中的 MSG,而不是文件系统中的 MSG

c# - .net 应用程序的生命周期是什么